Skip to main content

GraphQL

GraphQL runtime

While you can store users in the same database, it's wiser to avoid data duplication and re-use your service for user management available at GraphQLZero endpoint. Let's introduce the GraphQL runtime that allows remote GraphQL queries.

Update typegraph.py with the highlighted lines below:

Loading...

Again, a few interesting things happened here:

  1. No migration has been run. The field user comes from another runtime and doesn't exist in the database. The typegate will orchestrate the query execution in all runtimes and minimize the work done.
  2. The from_parent rule automatically fills the input type with the parent field named uid. The g(·) rule allows making named references to another type and avoids circular references.

Other type enforcement rules also exist:

  • from_secret(key) to fill the input type with the secret in the TG_[typegraph name]_[key] format
  • from_context(·) to fill the input type with content from the request context, such as JSON Web Token (JWT), etc.
  • set(x) to fill the input type with content x
  • The Entity which you fetch from the external API should have a matching name to that specified in the external API. Taking the above example, you need to specify the name(User) of the type the external API uses in your entity definition. As you can see, the name User is included in the user type declared in the typegraph. This is crucial as the query engine uses this information when making the external GraphQL call.
const user = t.struct({ "id": t.string(), "name": t.string() }, {name: "User"});

You should now start to see the power provided by Metatype and might wonder how to integrate it step by step with your existing systems. Writing all those types by hand is tedious and error-prone. The next section will show you how to generate types from existing sources.