graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Schema not exclude

Open canhbk opened this issue 7 years ago • 15 comments

Describe the bug Can't specify schema files to exclude

To Reproduce Steps to reproduce the behavior:

  • Generate at first time will working
  • Generate at second time will not work
√ Parse configuration
  > Generate outputs
    > Generate src/generated/graphql.generated.ts
      × Load GraphQL schemas
        → Failed to load schema
        Load GraphQL documents
        Generate


 Found 1 error

  × src/generated/graphql.generated.ts
    Failed to load schema from !**/*.generated.ts:

        Unable to find any GraphQL type defintions for the following pointers: ! 
**/*.generated.ts
        Error: Unable to find any GraphQL type defintions for the following poin 
ters: !**/*.generated.ts
    at Object.loadTypedefs 
(C:\Users\Canh\Documents\Project\Mandevices\mandevices\server\services\post\node 
_modules\graphql-toolkit\dist\commonjs\loaders\load-typedefs.js:78:15)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export " 
schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.
  1. My GraphQL schema:
type Query {
		posts: [Post!]!
	}
	type Post @key(fields: "id") {
		id: ID!
		title: String
	}
  1. My GraphQL operations:
Query: {
		posts: () => {
			return [
				{
					id: "abc",
					title: "Sach"
				}
			];
		}
	},
	Post: {
		// @ts-ignore
		__resolveReference(post, { fetchPostById }) {
			return fetchPostById(post.id);
		}
	}
  1. My codegen.yml config file:
overwrite: true
schema: 
  - './src/**/*.ts'
  - '!**/*.generated.ts'
documents: null
generates:
  src/generated/graphql.generated.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      federation: true
      noSchemaStitching: true

Expected behavior Files should be generated

Environment:

  • OS: Window 10 1903 build 18362.295
  • @graphql-codegen/...: 1..6.1
  • NodeJS: 10.15.3

canhbk avatar Aug 20 '19 12:08 canhbk

Weird, the generated files should be automatically ignored.

@CANHBK please note the following: it's not a good idea to specify all **/*.ts files - it will make the codegen work very hard. Try to be more specific.

Also, can you please try to use:

schema: 
  - './src/**/*.ts'
  - '!(src/**/*.generated.ts)'

?

dotansimha avatar Aug 27 '19 10:08 dotansimha

@dotansimha, Thanks for your answer

canhbk avatar Aug 29 '19 17:08 canhbk

I'm not opening another issue because I have a similar problem.

This is my codegen.yml:

overwrite: true
schema: "http://localhost:8080/graphql"
documents:
  - "src/web/schemas/public/*.graphql"
  - "src/web/schemas/private/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-resolvers"
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

And this is the output:

$ graphql-codegen --config codegen.yml
  √ Parse configuration
  > Generate outputs
    > Generate src/generated/graphql.ts
      √ Load GraphQL schemas
      × Load GraphQL documents
        → Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/*.graphql, src/web/schemas/private/*.graphql
        Generate
    > Generate ./graphql.schema.json
      √ Load GraphQL schemas
      × Load GraphQL documents
        → Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/*.graphql, src/web/schemas/private/*.graphql
        Generate

But when I do ls src/web/schemas/private/*.graphql, I see all the files there.

Note: I have multiple .graphql files which I'm merging with mergeSchemas, and I've sorted them into folders based on permission level, but this shouldn't interefere with finding the files in the first place.

OS: Windows 10 (using Cygwin to do the ls, but the file finder should be able to figure things out, right?)

Any clue, @dotansimha? Thanks!

dandrei avatar Sep 07 '19 12:09 dandrei

Hi @dandrei ! I'm not sure what causing this. Is it something you can share so I can reproduce it locally?

dotansimha avatar Sep 09 '19 14:09 dotansimha

Hi @dotansimha, I've stripped down the code to the bare minimum and uploaded it here: https://www.dropbox.com/s/89lhisjmdmuwc32/2019-09-09-gcg.zip?dl=0

Also tested on Linux to make sure this isn't OS specific.

After extracting the archive, all you need to do is:

  1. npm i
  2. npm run start

At this point, http://127.0.0.1:8080/graphql should be serving a GraphQL endpoint (if you visit it in the browser, you'll get the Apollo GraphQL Playground interactive query IDE).

The only query that works in this stripped down version is:

query {
  readData(name: "testing 123")
}

Running graphql-codegen --config codegen.yml (or npm run gen -- it's aliased in package.json) should reproduce the issue.

dandrei avatar Sep 09 '19 18:09 dandrei

Even specifying the path explicitly (without wildcards) in codegen.yml fails:

documents:
  - "src/web/schemas/public/public.graphql"

That errors out as:

Error: Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/public.graphql

But the file is clearly there.

dandrei avatar Sep 13 '19 10:09 dandrei

+1 experiencing the exact same behaviour as @dandrei

phillipwinder avatar Sep 18 '19 20:09 phillipwinder

+1

brabacpetr avatar Sep 19 '19 11:09 brabacpetr

Maybe it's related to the directory you are running it from? (the cwd) I'll try locally your reproduction and update soon.

dotansimha avatar Sep 19 '19 12:09 dotansimha

@dandrei
I don't understand your codegen.yml file. Under schema, you specify a url for GraphQL endpoint which should provide your schema. You also have local files for your schema, and you use it with documents field (which is for GraphQL operations documents).

You should first figure out where you schema is coming from. If you are writing the GraphQL server, you probably can load your schema files using local files (and then there is no need for url endpoint). If you are writing a client side, you can either load the schema from local file, or from endpoint. But your documents field should have value only of you are using client-side plugins in your client-side application, and it should point to GraphQL operations (query/mutation/fragment/subscription).

Also, the typescript plugin is a common plugin, but typescript-operations/typescript-react-apollo is for client-side only, and typescript-resolvers is for server-side only - they shouldn't get mixed.

@brabacpetr @PhillipWinder I'm not sure what's it your use-case and what causes it, but please add more info and a reproduction.

dotansimha avatar Sep 19 '19 13:09 dotansimha

Thanks @dotansimha.

I am interested in generating files for both the client side (Apollo React hooks and TypeScript types) and server side (TypeScript types and resolver signatures -- the last one mostly to check that my resolvers have the correct input and output types, useful especially when refactoring them).

I specified both a URL (under the schema field) and .graphql files (under the documents field) because that's how I saw it done in the configuration example here: https://graphql-code-generator.com/docs/getting-started/#using-in-runtime

Are you saying that I have to generate the server-side and client-side code in separate steps (so using two different codegen.yml configuration files)?

I did the following and it seems to work without throwing errors:

codegenc.yml (for client)

overwrite: true
schema: "http://localhost:8080/graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

codegens.yml (for server)

overwrite: true
schema: "http://localhost:8080/graphql"
generates:
  src/generated/graphql.ts:
    schema:
      - "src/web/schemas/public/public.graphql"
    plugins:
      - "typescript"
      - "typescript-resolvers"
  ./graphql.schema.json:
    plugins:
      - "introspection"

Haven't yet tested if the two generated files (graphql.ts, graphql.tsx) work the way I need them to, but at least I managed to generate them, which was the original problem.

dandrei avatar Sep 19 '19 14:09 dandrei

@dandrei As I see, you don't have documents in codegenc.yml file so you don't have any generated components. But, you should have documents with your operations defined to be used in the client app.

ardatan avatar Sep 19 '19 22:09 ardatan

You're right, I just can't seem to get it to not throw errors when I specify a documents field.

If the configuration file is this:

overwrite: true
documents:
  - "src/web/schemas/public/public.graphql"
schema: "http://localhost:8080/graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
  ./graphql.schema.json:
    plugins:
      - "introspection"

I get this:

graphql-codegen --config codegenc.yml
  √ Parse configuration
  > Generate outputs
    > Generate src/generated/graphql.tsx
      √ Load GraphQL schemas
      × Load GraphQL documents
        → Unable to find any GraphQL type definitions for the following pointers: src/web/schemas/public/public.graphql
        Generate

dandrei avatar Sep 20 '19 07:09 dandrei

@CANHBK Does that src/web/schemas/public/public.graphql file have operations like below;

query SomeQuery {
  foo {
    bar
  }
}

If yes, please open a new issue with a reproduction using this sandbox https://codesandbox.io/s/github/dotansimha/graphql-code-generator-issue-sandbox-template If no, I suggest you to check GraphQL docs to learn more about the difference between schema and documents.

ardatan avatar Sep 20 '19 10:09 ardatan

the spec is really confusing https://graphql.github.io/graphql-spec/June2018/#sec-Language.Document Here is a link to explain the differences https://stackoverflow.com/questions/59417171/whats-difference-between-schema-and-documents-in-graphql

saostad avatar Dec 19 '19 21:12 saostad