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

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.

Related

How can I create a NestJS graphql server with dynamic schema with respect to user?

I am using Nest JS for server and new to Graphql, I want to create a graphql server but schema defines in Graphql module in app.module. I am using schema first appraoch.
app.module
in importing Graphql Module, typepaths property defines to create typings from schemas present in any path.
but I don't have any particular schema because I want my user to enter any thing he want and fetch data using Graphql where typings should be with respect to particular user.
Things I tried:
I tried to rewrite the schema using filesystem methods from service but to update typings from schema nest server needs to restart to generate typings again.
please anyone give me a guide or any approach how can I achieve dynamic typings. I want a server which shows a graphql playground but user should be allow to query with respect to their data.
like for user 1 this highlighted box can be a schema but for different user this schema should be with respect to himself:user 1 should see this schema and should query only using this schema
Related Images are attached in link.
Any guide would be appreciated, Thanks!

Download Gitlab's graphql schema

I'm trying to get hold of gitlab_schema.graphql, i.e. the schema for Gitlab's graphql API. Can't find it anywhere, does anyone have any pointers?
You can use apollo CLI for this: apollo client:download-schema --endpoint=https://gitlab.yourcompany.com/api/graphql schema.graphql or just omit filename and it will download to JSON
It doesn't look like GitLab shares the schema directly, just this Reference Documentation generated from the schema, and the GraphiQL Explorer / IDE (more info about GraphiQL Explorer here).
I found the schema is an artifact in the Gitlab CI pipe (graphql-schema-dump job), for example here: https://gitlab.com/gitlab-org/gitlab/-/jobs/1602029144.
Not perfect, but works.
Here's alternative, if you are working with Rust lang. For Rust developers, you need to install the crate named graphql_client_cli via cargo. Example: cargo install graphql_client_cli.
Next, you can run the graphql client you just downloaded, to get the schema (either in JSON or GraphQL format). Example: graphql-client --authorization 'Bearer <bearer token>' https://<your gitlab url site>/api/graphql.
See the GraphQL Client CLI for more options.

Karate - auto updating request based on product app schema is possible?

I am working on validating the graphQL request and response for a POST method using Karate. Recently the schema undergone changes and now the requests/response changed.I need to re-work completely. Is there any way in updating the requests based on the schema in the product app while running in local other than manual updation?
No there isn't especially for GraphQL. The whole point of the test is to fail when your schema changes and think of this as Karate doing it's job.
If your response schemas are constant across multiple tests, you can choose to extract them into re-usable files: https://stackoverflow.com/a/56987803/143475
Also see: https://stackoverflow.com/a/56273812/143475
But you are generally recommended to keep your tests simple and readable, refer: https://stackoverflow.com/a/54126724/143475

Extract Graphql queries sent by a browser application with Apollo client

I am trying to simplify the process of exporting GraphQL queries sent by my application for documentation purposes. For the record, I want to be able to paste those queries into Postman collections.
Here are my different approaches:
Relying on .graphql files: first it's still very difficult to setup with a full fledged TypeScript + Webpack + Babel setup (using Next.js). Anyway, it does not provide variables, so you only have half the query.
Relying on the network tab. From there, we can copy queries content and import into Postman. Combined with Cypress it could provide an awesome workflow. It works OK, but Apollo Client will send queries as JSON objects, difficult to interpret
I tried to use the "application/graphql" content-type. It's way more readable and available in Postman. BUUUT it is non-standard, and thus not available in Apollo client.
So my question is rather open, but what are the possibilities to enable extracting graphql queries (and variables) sent by my browser and inject them into Postman?
Most promising solution is enabling "application/graphql" client side, or converting the JSON representation back to a string representation. But I could explore another possibility (eg using Apollo Engine as an intermediate)
A way to do this is to use the apollo CLI tool. It includes a client:extract command that extracts all of the GraphQL operations/documents in your application into a file. You run the tool on your JS(X)/TS(X) files and it extracts the GraphQL documents into a file that looks like this (this output is the result of pointing the tool at a single file containing a single query):
{
"version": 2,
"operations": [
{
"signature": "b4f318e6aebcc3631bc88eedef09c6001bb8c310917e97ee6df4a99e17c3c056",
"document": "query BootstrapQuery{user:viewer{__typename delivery_time_1 delivery_time_2 devices{__typename fcm_token id notification{__typename enabled}}has_password id label location name next_delivery_string oauths{__typename email id name picture provider}plan plan_billing_service plan_expires plan_since plan_will_renew profile_picture recovery_email timezone username}}",
"metadata": {
"engineSignature": ""
}
}
]
}
You can then use that file however you want.
In my case, I use this tool to publish an allow-list of operations to Hasura. I'm not sure what you mean by injecting queries into Postman, but I think this tool may provide you with an automated start that would be an improvement over manual copy/pasting.

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