I'm using graphql-codegen to generate typescript types from graphql schema. I'm trying to create a fragment with dynamic fields.
schema.ts
This is the type generated by graphql-codegen
.
/** User Type */
export type UserType = {
__typename?: 'UserType';
id: Scalars['ID'];
avatar: Scalars['String'];
email: Scalars['String'];
name: Scalars['String'];
showPostsInFeed: Scalars['Boolean'];
username: Scalars['String'];
};
user.model.ts
I'm using these interfaces throughout the entire application for validations and consistency.
export interface IUserBase {
id: string;
avatar: string;
name: string;
username: string;
}
export interface IUserPost extends IUserBase {
showPostsInFeed: boolean;
}
export interface IUserProfile extends IUserBase, IUserPost {
email: string;
}
show.ts
This is the file used for generation. Here I want to make a fragment with dynamic fields using my existent IUserPost
and IUserProfile
interfaces, in order to reuse those fields and avoid duplication repeating them inside the fragment one by one.
import gql from 'graphql-tag';
import { keys } from 'ts-transformer-keys';
import { IUserProfile, IUserPost } from '../../user.model';
const keysUserPofile = keys<IUserProfile>(); //Get all interface keys
const keysUserPost = keys<IUserPost>(); //Get all interface keys
//Fragments
export const fragments = {
userProfile: gql`
fragment UserProfile on UserType {
${keysUserPofile.join('\n')} //Interpolation
}
`,
userPost: gql`
fragment UserPost on UserType {
${keysUserPost.join('\n')} //Interpolation
}
`
};
//Queries
export const userProfileQuery = gql`
query UserProfileQuery($id: String!) {
showUser(id: $id) {
...UserProfile
}
}
`;
export const userPostQuery = gql`
query UserPostQuery($id: String!) {
showUser(id: $id) {
...UserPost
}
}
`;
When I try to pass these fields using interpolation, I get this error:
$ npm run generate
> gogofans-ui@0.0.0 generate C:\Development\GogoFans\gogofans-ui
> graphql-codegen --config codegen.yml
√ Parse configuration
> Generate outputs
> Generate src/app/core/graphql/schema.ts
√ Load GraphQL schemas
× Load GraphQL documents
→ Syntax Error: Expected Name, found "}".
Generate
Found 1 error
× C:/Development/GogoFans/gogofans-ui/src/app/users/graphql/fragments/show.ts
GraphQLError: Syntax Error: Expected Name, found "}".at syntaxError (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\error\syntaxError.js:15:10)
at Parser.expectToken (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1423:40)
at Parser.parseName (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:92:22)
at Parser.parseField (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:289:28)
at Parser.parseSelection (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:278:81)
at Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
at Parser.parseSelectionSet (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:265:24)
at Parser.parseFragmentDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:410:26)
at Parser.parseDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:134:23)
at Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
at Parser.parseDocument (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:109:25)
at Object.parse (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:36:17)
at Object.parseGraphQLSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\utils\index.cjs.js:601:28)
at parseSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:239:18)
at CodeFileLoader.load (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:173:28)
at async loadFile (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\load\index.cjs.js:48:24)GraphQLError: Syntax Error: Expected Name, found "}".
at syntaxError (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\error\syntaxError.js:15:10)
at Parser.expectToken (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1423:40)
at Parser.parseName (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:92:22)
at Parser.parseField (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:289:28)
at Parser.parseSelection (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:278:81)
at Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
at Parser.parseSelectionSet (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:265:24)
at Parser.parseFragmentDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:410:26)
at Parser.parseDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:134:23)
at Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
at Parser.parseDocument (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:109:25)
at Object.parse (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:36:17)
at Object.parseGraphQLSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\utils\index.cjs.js:601:28)
at parseSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:239:18)
at CodeFileLoader.load (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:173:28)
at async loadFile (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\load\index.cjs.js:48:24)Something went wrong
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gogofans-ui@0.0.0 generate:graphql-codegen --config codegen.yml
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gogofans-ui@0.0.0 generate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Fidel\AppData\Roaming\npm-cache_logs\2020-07-06T07_01_25_424Z-debug.log