Hey! I'm sure I must be doing some rookie mistake, but I'm struggling to get this work.
package.json
{
"dependencies": {
"@gql2ts/from-schema": "^1.10.1",
"apollo-server-express": "^2.1.0",
"graphql": "^0.13.2",
},
"devDependencies": {
"@gql2ts/types": "^1.9.0",
"@types/graphql": "^14.0.0",
"@types/node": "^10.10.3",
"gql2ts": "^1.10.1",
"ts-node": "^7.0.1",
"typescript": "^3.0.3"
}
}
I'm successfully able to generate the types from my GQL schemas
./scripts/generateTypes.ts
const typescriptTypes = generateNamespace("GQL", mySchema);
fs.writeFile(
path.join(__dirname, "./types/schema.d.ts"),
typescriptTypes,
err => {
console.log(err);
}
);
Output:
./types/schema.d.ts
// tslint:disable
// graphql typescript definitions
declare namespace GQL {
interface IGraphQLResponseRoot {
data?: IQuery | IMutation;
errors?: Array<IGraphQLResponseError>;
}
interface IGraphQLResponseError {
/** Required for all errors */
message: string;
locations?: Array<IGraphQLResponseErrorLocation>;
/** 7.2.2 says 'GraphQL servers may provide additional entries to error' */
[propName: string]: any;
}
interface IGraphQLResponseErrorLocation {
line: number;
column: number;
}
interface IQuery {
__typename: "Query";
me: IAccount;
}
interface IMutation {
__typename: "Mutation";
login: IAccount;
logout: boolean;
}
interface ILoginOnMutationArguments {
email: string;
password: string;
}
}
// tslint:enable
... but when I try to use them in my resolver:
resolver.ts
export const resolvers: ResolverMap = {
Mutation: {
login: async (parent, { email, password }: ILoginOnMutationArguments, { req }) => {
// ...
},
},
};
... I get the following error:
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/modules/resolver.ts(32,48): error TS2503: Cannot find namespace 'GQL'.
at createTSError (./node_modules/ts-node/src/index.ts:261:12)
at getOutput (./node_modules/ts-node/src/index.ts:367:40)
at Object.compile (./node_modules/ts-node/src/index.ts:558:11)
at Module.m._compile (./node_modules/ts-node/src/index.ts:439:43)
at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Object.require.extensions.(anonymous function) [as .ts] (./node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
Worth noting perhaps is that my IDE successfully recognizes the namespace, it fails on runtime only.
Hey! I'm sure I must be doing some rookie mistake, but I'm struggling to get this work.
package.json
I'm successfully able to generate the types from my GQL schemas
./scripts/generateTypes.ts
Output:
./types/schema.d.ts
... but when I try to use them in my resolver:
resolver.ts
... I get the following error:
Worth noting perhaps is that my IDE successfully recognizes the namespace, it fails on runtime only.