graphql-yoga - where are GraphQL resolver arguments defined and documented? - graphql

I've seen several sources describe the (root, args, context, info) arguments to a graphql-yoga resolver, but I am still searching for the real documentation of a graphql-yoga resolver. There are a few blog posts that mention the resolver arguments:
prisma blog - Feb 2017 - GraphQL Schemas, TypeDefs & Resolvers Explained
prisma blog - Feb 2018 - Demystifying the info Argument in GraphQL Resolvers
I know that prisma has built graphql-yoga on graphql.js via graphql-tools, and it appears that graphql.js documents these parameters at graphql.org - Root fields & resolvers, but it's not clear if graphql.org is the main place where graphql.js is documented, and even if it was, it is not clear to me that as a user of graphql-yoga I should defer to the documentation of graphql.js.
In addition, prisma has built graphql-yoga on apollo-server; is the documentation for apollo-server most applicable here?
It doesn't appear that these arguments are part of the GraphQL spec.
Where is the real documentation for the arguments to a graphql-yoga resolver? If that is not available and documented, where in the graphql-yoga source is the function that expects the arguments to take this shape?

TL;DR
For use with graphql-yoga, you probably want to follow apollo-server's docs for resolvers in the graphql-tools section for Resolver function signature.
Details
To know which docs to use, one needs to understand the full tech stack: prisma built stuff on top of tools by apollo, which in turn is built on top of the reference implementation of GraphQL, graphql.js. Each layer of this stack has a separate docs site.
You can follow the trail from the graphql-yoga README, which says it is built on apollo-server, whose own README in turn says:
Apollo Server works with any GraphQL schema built with GraphQL.js, so you can build your schema with that directly or with a convenience library such as graphql-tools.
So graphql-yoga is an opinionated, batteries-included form of apollo-server, which takes schemas from graphql.js but is likely to have more seamless integration with schemas specified with apollo's own graphql-tools.

Related

How to merge 2 graphql schemas on client, to auto-generate types?

I have a client that queries 2 endpoints. Now, how do I auto-generate types from 2 graphql schemas, when using 2 endpoints?
Scripts I used up until now (with standard 1 endpoint) uses Apollo codegen (source):
"schema": "npx apollo service:download --endpoint=http://localhost:8080/graphql graphql-schema.json",
"types": "npm run schema && apollo client:codegen --localSchemaFile=graphql-schema.json --variant=development --target=typescript --addTypename --queries=./src/**/*.graphql --useReadOnlyTypes --globalTypesFile=src/globalTypes.ts . && npm run prettier"
Possible solution is also generating 2 introspection schemas and then merging them together, I just didn't find how.
I'm also open to move to Graphql Code Generator.
Is there any example, guidance, or link please?
Thank you
GraphQL Code Generator supports configuring multiple schemas by merging their definitions automatically for you.
You will find some documentation on this matter here: https://www.the-guild.dev/graphql/codegen/docs/config-reference/schema-field#multiple-schemas-and-client-side-schema
Please note that schema can take an array of schema file paths or URLs.
Let us know if you need any guidance migrating from Apollo codegen.
Charly, from The Guild.

How to log calls to deprecated fields in apollo server

We marked some fields in our schema using the #deprecated directive. Now we want to log if these fields are still in use from some of our clients. What would be the best way to do this, without using Apollo Studio.
If you have access to the client code, then you can utilize GraphQL Inspector to check for deprecated usage. Using the CLI, you just do:
graphql-inspector validate DOCUMENTS SCHEMA
where DOCUMENTS is a glob pattern used to match the files containing the queries and SCHEMA is a pointer to the schema used for validation. The files containing the queries can be .graphql files or .js/.ts files. The schema pointer can be a URL to your schema or one or more .graphql files with your schema's type definitions. See here and here for additional ways to provide the schema and documents.
If you don't have access to the client code, or specifically need to log deprecated usage on every request, then you can write your own Apollo Server plugin and utilize GraphQL Inspector's programmatic API instead to validate each request's parsed document as it comes in. The parsed document will be available beginning with the validationDidStart lifecycle hook. See the docs for a complete example of how to write your own plugin.

Is TypeGraphql with Apollo federation possible?

We are planning to migrate towards Apollo GraphQL, because of the Apollo federation feature which allows unifying multiple microservices behind a single GraphQL API. However we are using TypeGraphQL at the moment, and I believe them to be incompatible. The reason is that in Apollo federation I see you use special #key property
/* example from docs */
const typeDefs = gql`
type Query {
me: User
}
type User #key(fields: "id") {
id: ID!
username: String
}
`;
However, the schema for TypeGraphql is automatically generated from classes. Is there a way to still use both technologies, or they are mutually exclusive?
Typegraphql had added the support for it, check the examples here https://github.com/MichalLytek/type-graphql/tree/master/examples/apollo-federation
While it has support by using #Directive decorator, it's not quite easy to work with it on a large scale project. I'm currently implementing it on a complex project, and I'm not satisfied with the amount of boilerplate code needed to reference some type from another schema. There was some conversation about adding better support here:
https://github.com/MichalLytek/type-graphql/issues/351#issuecomment-549156066
I'm planning to add support if I will have time soon.
but it's still not implemented. I've created a repo where you can see minimal example (similar to one in type-graphql repo):
https://github.com/hrvojepavlinovic/apollo-federation-type-graphql

Expected Schema {...} to be a GraphQL schema

Trying to setup subscriptions through apollo, both on the backend and frontend. The error arises when trying to call subscribeToMore function of the Query component. Although it clearly says that the schema is not a GraphQL schema, was not able to find any issues.
I have a suspicion, since I am using merge-graphql-schemas to merge typeDefs and revolvers and passing the merged schema to SubscriptionServer it some how doesnt stitch it together with the Subscription Operation.
I have created a gist of all the related pieces.
TIA.
So I finally figured out the issue.
It indeed had to with merge-graphql-schemas library but due to my fault.
After going through docs, that said
Beware that mergeResolvers is simply merging plain Javascript objects together. This means that you should be careful with Queries, Mutations or Subscriptions with naming conflicts.
Which also showed on console logging.
So it comes out that there are different options depending on the server implementation.
So making the schema using const schema = makeExecutableSchema({ typeDefs, resolvers });
helped resolve my problem.
Intially I tried using const schema = buildSchema(typeDefs); but for some reason it didnt stitch the resolvers and they stopped from firing.

How to build relationship map?

I like GraphQL and another libraries around GraphQL. I like GraphQL tools. But I found that I need a relation map. I thought that such tool had to exist. But I have not found. Does it exist?
If you are asking for a visual representation of relations, you could use GraphQL introspection to achieve this. Take a look on GraphQL Voyager:
Demo:
https://apis.guru/graphql-voyager/
Copy the introspection query, paste and run it on your GraphQL API endpoint.Copy the results into GraphQL Voyager. You should get a nice visual representation of your data relations available through your GraphQL API.
Github:
https://github.com/APIs-guru/graphql-voyager

Resources