Intended outcome:
I ran apollo client:extract and did not expect files under src/__tests__ to be included, like the docs say.
Actual outcome:
They are included.
I'm pretty sure the **/node_modules and **/__tests__ that are in DefaultConfigBase just don't do anything and need to be **/node_modules/** and **/__tests__/** instead?
How to reproduce the issue:
glasser@glasser-mbp 0 09:40:55 /tmp $ mkdir z
glasser@glasser-mbp 0 09:41:05 /tmp $ cd z
/tmp/z
glasser@glasser-mbp 0 09:41:05 /tmp/z $ npm install apollo
npm WARN deprecated [email protected]: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
> [email protected] postinstall /private/tmp/z/node_modules/babel-polyfill/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
> [email protected] postinstall /private/tmp/z/node_modules/babel-runtime/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
> [email protected] postinstall /private/tmp/z/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"
npm WARN saveError ENOENT: no such file or directory, open '/private/tmp/z/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/private/tmp/z/package.json'
npm WARN [email protected] requires a peer of typescript@>=2.0 but none is installed. You must install peer dependencies yourself.
npm WARN z No description
npm WARN z No repository field.
npm WARN z No README data
npm WARN z No license field.
+ [email protected]
added 307 packages from 226 contributors and audited 3233 packages in 9.818s
found 0 vulnerabilities
glasser@glasser-mbp 0 09:41:19 /tmp/z $ mkdir -p src/__tests__
glasser@glasser-mbp 0 09:41:25 /tmp/z $ echo 'gql`query Foo { bar }`' >src/__tests__/foo.ts
glasser@glasser-mbp 0 09:41:55 /tmp/z $ echo 'gql`query Foo { bar }`' >src/nonTest.ts
glasser@glasser-mbp 0 09:42:27 /tmp/z $ echo 'type Query { bar: Int }' >schema.graphql
glasser@glasser-mbp 0 09:42:53 /tmp/z $ echo 'module.exports = {client: {service: {localSchemaFile: "schema.graphql"}}}' >apollo.config.js
glasser@glasser-mbp 0 09:43:25 /tmp/z $ npx apollo client:extract --localSchemaFile=schema.graphql
Error: ️️There are multiple definitions for the Foo operation. All operations in a project must have unique names. If generating types, only the types for the first definition found will be generated.
at GraphQLClientProject.checkForDuplicateOperations (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:129:31)
at GraphQLClientProject.documentDidChange (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:119:14)
at GraphQLClientProject.fileDidChange (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:100:18)
at /private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:87:22
at GraphQLClientProject.scanAllIncludedFiles (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:89:11)
at GraphQLClientProject.initialize (/private/tmp/z/node_modules/apollo-language-server/lib/project/client.js:60:22)
at new GraphQLProject (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:39:46)
at new GraphQLClientProject (/private/tmp/z/node_modules/apollo-language-server/lib/project/client.js:35:9)
at ClientExtract.createService (/private/tmp/z/node_modules/apollo/lib/Command.js:127:28)
at ClientExtract.init (/private/tmp/z/node_modules/apollo/lib/Command.js:44:14)
✔ Loading Apollo Project
✔ Extracting operations from project
✔ Outputing extracted queries to --localSchemaFile=schema.graphql
Successfully wrote 1 operations from the undefined client to --localSchemaFile=schema.graphql
glasser@glasser-mbp 0 09:44:48 /tmp/z $ npx apollo client:extract --localSchemaFile=schema.graphql --excludes='**/__tests__'
Error: ️️There are multiple definitions for the Foo operation. All operations in a project must have unique names. If generating types, only the types for the first definition found will be generated.
at GraphQLClientProject.checkForDuplicateOperations (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:129:31)
at GraphQLClientProject.documentDidChange (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:119:14)
at GraphQLClientProject.fileDidChange (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:100:18)
at /private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:87:22
at GraphQLClientProject.scanAllIncludedFiles (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:89:11)
at GraphQLClientProject.initialize (/private/tmp/z/node_modules/apollo-language-server/lib/project/client.js:60:22)
at new GraphQLProject (/private/tmp/z/node_modules/apollo-language-server/lib/project/base.js:39:46)
at new GraphQLClientProject (/private/tmp/z/node_modules/apollo-language-server/lib/project/client.js:35:9)
at ClientExtract.createService (/private/tmp/z/node_modules/apollo/lib/Command.js:127:28)
at ClientExtract.init (/private/tmp/z/node_modules/apollo/lib/Command.js:44:14)
✔ Loading Apollo Project
✔ Extracting operations from project
✔ Outputing extracted queries to --localSchemaFile=schema.graphql
Successfully wrote 1 operations from the undefined client to --localSchemaFile=schema.graphql
glasser@glasser-mbp 0 09:43:27 /tmp/z $ npx apollo client:extract --localSchemaFile=schema.graphql --excludes='**/__tests__/**'
✔ Loading Apollo Project
✔ Extracting operations from project
✔ Outputing extracted queries to --localSchemaFile=schema.graphql
Successfully wrote 1 operations from the undefined client to --localSchemaFile=schema.graphql
As you can see, the default excludes value fails to actually exclude the duplicate Foo, and an explicit excludes with the same default value has the same issue, but an explicit excludes with a trailing /** works.
Versions
2.21.1.
I met with the same problem. I'm using graphql-code-generator and .graphql files. So it conflicts when find operations with the same names in .graphql files and generated files with gql tag.
My workaround:
module.exports = {
client: {
service: {
name: 'client',
localSchemaFile: './schema.graphql',
},
tagName: 'omitGQLtags',
},
};
Working demo repo with .graphql files, graphql-code-generator and Apollo GraphQL (vs code plugin): https://github.com/nodkz/example-apollo3
PS. I dig into to apollo-language-server and it does not support exclude, include options for local schema definition:
https://github.com/apollographql/apollo-tooling/blob/938dd253c8ca7228eac44f37fbbd357476d6546b/packages/apollo-language-server/src/config/config.ts#L29-L32
@nodkz I have the exact same issue - thanks for posting the workaround! Spend the afternoon trying to figure out why it complain with duplicate queries even though I excluded everything possible.
If this tagName is enabled it will not offer intellisense but I can rename it when editing to omitGQLtags... and then rename it back. Syntax highlighting will not work but intellisense.
Hope this gets fixed :-)
But from the TS side it does have the definitions in the ConfigBase which the ClientConfigFormat extends.
export const DefaultConfigBase = {
includes: ["src/**/*.{ts,tsx,js,jsx,graphql,gql}"],
excludes: ["**/node_modules", "**/__tests__"]
};
export interface ConfigBase {
includes: string[];
excludes: string[];
}
export interface LocalServiceConfig {
name: ServiceID;
localSchemaFile: string | string[];
}
export type ClientServiceConfig = RemoteServiceConfig | LocalServiceConfig;
export interface ClientConfigFormat extends ConfigBase {
// service linking
service?: ServiceSpecifier | ClientServiceConfig;
...
}
Note taht includes and excludes are not at the service level but at the parent (ClientConfigFormat):
module.exports = {
client: {
includes: [`${__dirname}/@app/client/src/**/*.graphql`],
service: {
name: "postgraphile",
localSchemaFile: `${__dirname}/data/schema.graphql`,
},
},
};
@eleasarchriso you may try to play with my demo repo which has all configurations for .graphql files, graphql-code-generator and Apollo GraphQL (vs code plugin): https://github.com/nodkz/example-apollo3
We are now migrating all our app to this approach.
@nodkz very nice - thanks! So in your solution intellisense is working (for me):
gql toomitGqlTagsTheyAreUnderGraphqlCodeGeneratorControl (but syntax highlighting is then broken)Go to definition is not working in any file though. Not even internally in the schema.graphql file itself to navigate to e.g. Category
Is this the same for you as well or do you have nicer support for intellisense?
Most helpful comment
I met with the same problem. I'm using
graphql-code-generatorand.graphqlfiles. So it conflicts when find operations with the same names in.graphqlfiles and generated files withgqltag.My workaround:
Working demo repo with
.graphqlfiles,graphql-code-generatorandApollo GraphQL (vs code plugin): https://github.com/nodkz/example-apollo3PS. I dig into to apollo-language-server and it does not support
exclude,includeoptions for local schema definition:https://github.com/apollographql/apollo-tooling/blob/938dd253c8ca7228eac44f37fbbd357476d6546b/packages/apollo-language-server/src/config/config.ts#L29-L32