-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples(with-typescript-graphql): migrate to Yoga v3 and codegen new…
… `preset: client` (#41597) Co-authored-by: Balázs Orbán <[email protected]>
- Loading branch information
1 parent
fedcae4
commit 792c661
Showing
12 changed files
with
279 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { CodegenConfig } from '@graphql-codegen/cli' | ||
|
||
const config: CodegenConfig = { | ||
schema: [ | ||
{ | ||
'lib/schema.ts': { | ||
noRequire: true, | ||
}, | ||
}, | ||
], | ||
documents: './pages/**/*.tsx', | ||
generates: { | ||
'./lib/gql/': { | ||
preset: 'client', | ||
plugins: [], | ||
}, | ||
'./lib/resolvers-types.ts': { | ||
plugins: ['typescript', 'typescript-resolvers'], | ||
}, | ||
}, | ||
} | ||
|
||
export default config |
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
examples/with-typescript-graphql/lib/documents/updateName.graphql
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
examples/with-typescript-graphql/lib/documents/viewer.graphql
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
examples/with-typescript-graphql/lib/gql/fragment-masking.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core' | ||
|
||
export type FragmentType<TDocumentType extends DocumentNode<any, any>> = | ||
TDocumentType extends DocumentNode<infer TType, any> | ||
? TType extends { ' $fragmentName'?: infer TKey } | ||
? TKey extends string | ||
? { ' $fragmentRefs'?: { [key in TKey]: TType } } | ||
: never | ||
: never | ||
: never | ||
|
||
// return non-nullable if `fragmentType` is non-nullable | ||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: FragmentType<DocumentNode<TType, any>> | ||
): TType | ||
// return nullable if `fragmentType` is nullable | ||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: FragmentType<DocumentNode<TType, any>> | null | undefined | ||
): TType | null | undefined | ||
// return array of non-nullable if `fragmentType` is array of non-nullable | ||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: ReadonlyArray<FragmentType<DocumentNode<TType, any>>> | ||
): ReadonlyArray<TType> | ||
// return array of nullable if `fragmentType` is array of nullable | ||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: | ||
| ReadonlyArray<FragmentType<DocumentNode<TType, any>>> | ||
| null | ||
| undefined | ||
): ReadonlyArray<TType> | null | undefined | ||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: | ||
| FragmentType<DocumentNode<TType, any>> | ||
| ReadonlyArray<FragmentType<DocumentNode<TType, any>>> | ||
| null | ||
| undefined | ||
): TType | ReadonlyArray<TType> | null | undefined { | ||
return fragmentType as any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* eslint-disable */ | ||
import * as types from './graphql' | ||
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core' | ||
|
||
const documents = { | ||
'\n mutation UpdateName($name: String!) {\n updateName(name: $name) {\n id\n name\n status\n }\n }\n': | ||
types.UpdateNameDocument, | ||
'\n query Viewer {\n viewer {\n id\n name\n status\n }\n }\n': | ||
types.ViewerDocument, | ||
} | ||
|
||
export function graphql( | ||
source: '\n mutation UpdateName($name: String!) {\n updateName(name: $name) {\n id\n name\n status\n }\n }\n' | ||
): typeof documents['\n mutation UpdateName($name: String!) {\n updateName(name: $name) {\n id\n name\n status\n }\n }\n'] | ||
export function graphql( | ||
source: '\n query Viewer {\n viewer {\n id\n name\n status\n }\n }\n' | ||
): typeof documents['\n query Viewer {\n viewer {\n id\n name\n status\n }\n }\n'] | ||
|
||
export function graphql(source: string): unknown | ||
export function graphql(source: string) { | ||
return (documents as any)[source] ?? {} | ||
} | ||
|
||
export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = | ||
TDocumentNode extends DocumentNode<infer TType, any> ? TType : never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* eslint-disable */ | ||
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core' | ||
export type Maybe<T> = T | null | ||
export type InputMaybe<T> = Maybe<T> | ||
export type Exact<T extends { [key: string]: unknown }> = { | ||
[K in keyof T]: T[K] | ||
} | ||
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { | ||
[SubKey in K]?: Maybe<T[SubKey]> | ||
} | ||
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { | ||
[SubKey in K]: Maybe<T[SubKey]> | ||
} | ||
/** All built-in and custom scalars, mapped to their actual values */ | ||
export type Scalars = { | ||
ID: string | ||
String: string | ||
Boolean: boolean | ||
Int: number | ||
Float: number | ||
} | ||
|
||
export type Mutation = { | ||
__typename?: 'Mutation' | ||
updateName: User | ||
} | ||
|
||
export type MutationUpdateNameArgs = { | ||
name: Scalars['String'] | ||
} | ||
|
||
export type Query = { | ||
__typename?: 'Query' | ||
viewer: User | ||
} | ||
|
||
export type User = { | ||
__typename?: 'User' | ||
id: Scalars['ID'] | ||
name: Scalars['String'] | ||
status: Scalars['String'] | ||
} | ||
|
||
export type UpdateNameMutationVariables = Exact<{ | ||
name: Scalars['String'] | ||
}> | ||
|
||
export type UpdateNameMutation = { | ||
__typename?: 'Mutation' | ||
updateName: { __typename?: 'User'; id: string; name: string; status: string } | ||
} | ||
|
||
export type ViewerQueryVariables = Exact<{ [key: string]: never }> | ||
|
||
export type ViewerQuery = { | ||
__typename?: 'Query' | ||
viewer: { __typename?: 'User'; id: string; name: string; status: string } | ||
} | ||
|
||
export const UpdateNameDocument = { | ||
kind: 'Document', | ||
definitions: [ | ||
{ | ||
kind: 'OperationDefinition', | ||
operation: 'mutation', | ||
name: { kind: 'Name', value: 'UpdateName' }, | ||
variableDefinitions: [ | ||
{ | ||
kind: 'VariableDefinition', | ||
variable: { kind: 'Variable', name: { kind: 'Name', value: 'name' } }, | ||
type: { | ||
kind: 'NonNullType', | ||
type: { | ||
kind: 'NamedType', | ||
name: { kind: 'Name', value: 'String' }, | ||
}, | ||
}, | ||
}, | ||
], | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'Field', | ||
name: { kind: 'Name', value: 'updateName' }, | ||
arguments: [ | ||
{ | ||
kind: 'Argument', | ||
name: { kind: 'Name', value: 'name' }, | ||
value: { | ||
kind: 'Variable', | ||
name: { kind: 'Name', value: 'name' }, | ||
}, | ||
}, | ||
], | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ kind: 'Field', name: { kind: 'Name', value: 'id' } }, | ||
{ kind: 'Field', name: { kind: 'Name', value: 'name' } }, | ||
{ kind: 'Field', name: { kind: 'Name', value: 'status' } }, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as unknown as DocumentNode<UpdateNameMutation, UpdateNameMutationVariables> | ||
export const ViewerDocument = { | ||
kind: 'Document', | ||
definitions: [ | ||
{ | ||
kind: 'OperationDefinition', | ||
operation: 'query', | ||
name: { kind: 'Name', value: 'Viewer' }, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ | ||
kind: 'Field', | ||
name: { kind: 'Name', value: 'viewer' }, | ||
selectionSet: { | ||
kind: 'SelectionSet', | ||
selections: [ | ||
{ kind: 'Field', name: { kind: 'Name', value: 'id' } }, | ||
{ kind: 'Field', name: { kind: 'Name', value: 'name' } }, | ||
{ kind: 'Field', name: { kind: 'Name', value: 'status' } }, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
} as unknown as DocumentNode<ViewerQuery, ViewerQueryVariables> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './gql' | ||
export * from './fragment-masking' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { createServer } from '@graphql-yoga/node' | ||
import { createSchema, createYoga } from 'graphql-yoga' | ||
import gql from 'graphql-tag' | ||
|
||
import resolvers from 'lib/resolvers' | ||
import typeDefs from 'lib/schema' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
const server = createServer({ | ||
schema: { | ||
typeDefs: gql(typeDefs), | ||
resolvers, | ||
}, | ||
endpoint: '/api/graphql', | ||
// graphiql: false // uncomment to disable GraphiQL | ||
const schema = createSchema({ | ||
typeDefs: gql(typeDefs), | ||
resolvers, | ||
}) | ||
|
||
export default server | ||
export default createYoga<{ | ||
req: NextApiRequest | ||
res: NextApiResponse | ||
}>({ | ||
schema, | ||
// Needed to be defined explicitly because our endpoint lives at a different path other than `/graphql` | ||
graphqlEndpoint: '/api/graphql', | ||
}) |
Oops, something went wrong.