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:
- 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. - The
from_parent
rule automatically fills the input type with the parent field nameduid
. Theg(·)
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 theTG_[typegraph name]_[key]
formatfrom_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 contentx
- 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.