valu-digital / graphql-codegen-persisted-query-ids Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://npm.im/graphql-codegen-persisted-query-ids
License: MIT License
Home Page: https://npm.im/graphql-codegen-persisted-query-ids
License: MIT License
We use GraphQL and Apollo Client 3.x including some client-side generated fields using the @client directive in our .GQL files. Those @client fields are included in the server-side query which is a mistake -- they should be skipped because the server knows nothing about them. I fixed this for our case with a simple preprocessing step (see below) but it really should just be a configuration: omitFieldsDirectives: @client
or even just understand @client natively.
Here's my addToPersistedOperations.js post-generation script that creates the files without the @client fields.
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
// addToPersistedOperations.js
// TODO:BENJIE:: @client directive fields should be omitted
const map = require('../api/server-persisted-queries.json')
const fs = require('fs')
const fsp = fs.promises
async function main() {
if (
!fs.existsSync(`${__dirname}/../api/build/api/src/persisted_operations/`)
) {
fs.mkdirSync(
`${__dirname}/../api/build/api/src/persisted_operations/`,
// eslint-disable-next-line no-octal
0744
)
}
await Promise.all(
Object.entries(map).map(([hash, query]) => {
// We used @client directive, but the graphql-codegen-persisted-ids plugin does not
// know about the @client. Luckily, all we have to do is remove those fields from
// the server-side notion of the query (that means that the server-side hash will not
// be accurate but that does not matter (at all I hope))
const noClientDirectiveQuery = query.replace(
/\n\s*\w+\s+@client\s*\n/g,
'\n'
)
fsp.writeFile(
`${__dirname}/../api/build/api/src/persisted_operations/${hash}.graphql`,
noClientDirectiveQuery
)
})
)
}
main().catch(e => {
console.error(e)
process.exit(1)
})
I recently tried updating a project that uses this plugin, however found that it did not work with the latest @graphql-codegen/cli version or anything above 3.0.0.
I get the following error:
✖ Invalid Codegen Configuration!
Please make sure that your codegen config file has defined plugins list for output "dist/persisted_queri…
It should looks like that:
schema:
- my-schema.graphql
generates:
my-file.ts:
- plugin1
- plugin2
- plugin3
◼ Generate outputs
The config I am using is as follows
schema: "../../data/schema.graphql"
generates:
gen/client.tsx:
plugins:
- add:
content: "/* DO NOT EDIT! This file is auto-generated by graphql-code-generator - see `codegen.yml` */"
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
dist/persisted_queries/client.json:
- graphql-codegen-persisted-query-ids:
output: client
algorithm: sha256
On a project created with create-react-app, I get the following error when starting or building the app:
Incriminated code:
// src/config/apollo-client.ts
import { ApolloClient, createHttpLink } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import { createPersistedQueryLink } from "@apollo/client/link/persisted-queries";
import { usePregeneratedHashes } from "graphql-codegen-persisted-query-ids/lib/apollo";
import hashes from "../gql/persisted-query-ids/client.json";
import { apolloCache } from "./apollo-cache";
export default function initApolloClient(session: any) {
const httpLink = createHttpLink({
uri: window.ENV.GRAPHQL_SERVER_URL,
});
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: `Bearer ${session.appToken}`,
},
};
});
const persistedQueryLink = createPersistedQueryLink({
useGETForHashedQueries: false,
// eslint-disable-next-line
generateHash: usePregeneratedHashes(hashes),
});
return new ApolloClient({
link: authLink.concat(persistedQueryLink).concat(httpLink),
cache: apolloCache,
});
}
How can I remove automatic generated __typename
from server.json
queries?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.