Giter VIP home page Giter VIP logo

graphql-codegen-persisted-query-ids's People

Contributors

benjie avatar blenderdude avatar esamattis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

graphql-codegen-persisted-query-ids's Issues

GQL @client directive fields should be elided for server-side consumption

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)
})

Incompatibility with `@graphql-codegen/cli` version 3 and above

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

Miss configured sourcemaps ?

On a project created with create-react-app, I get the following error when starting or building the app:

Capture d’écran 2023-01-11 à 12 06 02

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,
  });
}

Disable __typename

How can I remove automatic generated __typename from server.json queries?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.