Last active
September 8, 2022 03:55
-
-
Save tatsuyasusukida/505c1ba248be6d57f7b5d8a6a19a271b to your computer and use it in GitHub Desktop.
Next.js + Apollo + Nexus.js + GraphQL Code Generator example
This file contains hidden or 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
import '../styles/globals.css' | |
import type { AppProps } from 'next/app' | |
import { Provider, createClient } from 'urql' | |
const client = createClient({ | |
url: '/api/graphql', | |
}) | |
function MyApp({ Component, pageProps }: AppProps) { | |
return ( | |
<Provider value={client}> | |
<Component {...pageProps} /> | |
</Provider> | |
) | |
} | |
export default MyApp |
This file contains hidden or 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
import { NextPage } from "next" | |
import { json } from "stream/consumers"; | |
import { gql, useQuery } from "urql"; | |
import { DraftsDocument } from "../generated/codegen"; | |
export const query = gql` | |
query Drafts { | |
drafts { | |
id | |
title | |
body | |
published | |
} | |
} | |
` | |
const Client: NextPage = () => { | |
const [result] = useQuery({ query: DraftsDocument }) | |
return ( | |
<pre> | |
{JSON.stringify(result?.data?.drafts, null, 2)} | |
</pre> | |
) | |
} | |
export default Client |
This file contains hidden or 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
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 Post = { | |
__typename?: 'Post'; | |
body?: Maybe<Scalars['String']>; | |
id?: Maybe<Scalars['Int']>; | |
published?: Maybe<Scalars['Boolean']>; | |
title?: Maybe<Scalars['String']>; | |
}; | |
export type Query = { | |
__typename?: 'Query'; | |
drafts: Array<Maybe<Post>>; | |
}; | |
export type DraftsQueryVariables = Exact<{ [key: string]: never; }>; | |
export type DraftsQuery = { __typename?: 'Query', drafts: Array<{ __typename?: 'Post', id?: number | null, title?: string | null, body?: string | null, published?: boolean | null } | null> }; | |
export const DraftsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Drafts"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"drafts"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"body"}},{"kind":"Field","name":{"kind":"Name","value":"published"}}]}}]}}]} as unknown as DocumentNode<DraftsQuery, DraftsQueryVariables>; |
This file contains hidden or 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
overwrite: true | |
schema: "generated/schema.graphql" | |
documents: "pages/**/*.tsx" | |
generates: | |
generated/codegen.ts: | |
plugins: | |
- "typescript" | |
- "typescript-operations" | |
- "typed-document-node" |
This file contains hidden or 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
import { ApolloServer } from "apollo-server-micro"; | |
import { NextApiRequest, NextApiResponse, PageConfig } from "next"; | |
import schema from "../../api/schema"; | |
const server = new ApolloServer({ schema }) | |
const serverHandler = server.start() | |
.then(() => server.createHandler({ path: '/api/graphql' })) | |
export default async function handler( | |
req: NextApiRequest, | |
res: NextApiResponse, | |
) { | |
res.setHeader('Access-Control-Allow-Origin', '*') | |
res.setHeader('Access-Control-Allow-Headers', 'content-type') | |
if (req.method === 'OPTIONS') { | |
res.status(200).end() | |
return | |
} | |
await (await serverHandler)(req, res) | |
} | |
export const config: PageConfig = { | |
api: { | |
bodyParser: false, | |
}, | |
} |
This file contains hidden or 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
export * from './post' |
This file contains hidden or 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 was generated by Nexus Schema | |
* Do not make changes to this file directly | |
*/ | |
declare global { | |
interface NexusGen extends NexusGenTypes {} | |
} | |
export interface NexusGenInputs { | |
} | |
export interface NexusGenEnums { | |
} | |
export interface NexusGenScalars { | |
String: string | |
Int: number | |
Float: number | |
Boolean: boolean | |
ID: string | |
} | |
export interface NexusGenObjects { | |
Post: { // root type | |
body?: string | null; // String | |
id?: number | null; // Int | |
published?: boolean | null; // Boolean | |
title?: string | null; // String | |
} | |
Query: {}; | |
} | |
export interface NexusGenInterfaces { | |
} | |
export interface NexusGenUnions { | |
} | |
export type NexusGenRootTypes = NexusGenObjects | |
export type NexusGenAllTypes = NexusGenRootTypes & NexusGenScalars | |
export interface NexusGenFieldTypes { | |
Post: { // field return type | |
body: string | null; // String | |
id: number | null; // Int | |
published: boolean | null; // Boolean | |
title: string | null; // String | |
} | |
Query: { // field return type | |
drafts: Array<NexusGenRootTypes['Post'] | null>; // [Post]! | |
} | |
} | |
export interface NexusGenFieldTypeNames { | |
Post: { // field return type name | |
body: 'String' | |
id: 'Int' | |
published: 'Boolean' | |
title: 'String' | |
} | |
Query: { // field return type name | |
drafts: 'Post' | |
} | |
} | |
export interface NexusGenArgTypes { | |
} | |
export interface NexusGenAbstractTypeMembers { | |
} | |
export interface NexusGenTypeInterfaces { | |
} | |
export type NexusGenObjectNames = keyof NexusGenObjects; | |
export type NexusGenInputNames = never; | |
export type NexusGenEnumNames = never; | |
export type NexusGenInterfaceNames = never; | |
export type NexusGenScalarNames = keyof NexusGenScalars; | |
export type NexusGenUnionNames = never; | |
export type NexusGenObjectsUsingAbstractStrategyIsTypeOf = never; | |
export type NexusGenAbstractsUsingStrategyResolveType = never; | |
export type NexusGenFeaturesConfig = { | |
abstractTypeStrategies: { | |
isTypeOf: false | |
resolveType: true | |
__typename: false | |
} | |
} | |
export interface NexusGenTypes { | |
context: any; | |
inputTypes: NexusGenInputs; | |
rootTypes: NexusGenRootTypes; | |
inputTypeShapes: NexusGenInputs & NexusGenEnums & NexusGenScalars; | |
argTypes: NexusGenArgTypes; | |
fieldTypes: NexusGenFieldTypes; | |
fieldTypeNames: NexusGenFieldTypeNames; | |
allTypes: NexusGenAllTypes; | |
typeInterfaces: NexusGenTypeInterfaces; | |
objectNames: NexusGenObjectNames; | |
inputNames: NexusGenInputNames; | |
enumNames: NexusGenEnumNames; | |
interfaceNames: NexusGenInterfaceNames; | |
scalarNames: NexusGenScalarNames; | |
unionNames: NexusGenUnionNames; | |
allInputTypes: NexusGenTypes['inputNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['scalarNames']; | |
allOutputTypes: NexusGenTypes['objectNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['unionNames'] | NexusGenTypes['interfaceNames'] | NexusGenTypes['scalarNames']; | |
allNamedTypes: NexusGenTypes['allInputTypes'] | NexusGenTypes['allOutputTypes'] | |
abstractTypes: NexusGenTypes['interfaceNames'] | NexusGenTypes['unionNames']; | |
abstractTypeMembers: NexusGenAbstractTypeMembers; | |
objectsUsingAbstractStrategyIsTypeOf: NexusGenObjectsUsingAbstractStrategyIsTypeOf; | |
abstractsUsingStrategyResolveType: NexusGenAbstractsUsingStrategyResolveType; | |
features: NexusGenFeaturesConfig; | |
} | |
declare global { | |
interface NexusGenPluginTypeConfig<TypeName extends string> { | |
} | |
interface NexusGenPluginInputTypeConfig<TypeName extends string> { | |
} | |
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> { | |
} | |
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> { | |
} | |
interface NexusGenPluginSchemaConfig { | |
} | |
interface NexusGenPluginArgConfig { | |
} | |
} |
This file contains hidden or 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
import { extendType, objectType } from "nexus" | |
export const Post = objectType({ | |
name: 'Post', | |
definition(t) { | |
t.int('id') | |
t.string('title') | |
t.string('body') | |
t.boolean('published') | |
}, | |
}) | |
export const PostQuery = extendType({ | |
type: 'Query', | |
definition(t) { | |
t.nonNull.list.field('drafts', { | |
type: 'Post', | |
resolve() { | |
return [ | |
{ id: 1, title: 'title', body: 'body', published: false }, | |
] | |
} | |
}) | |
}, | |
}) |
This file contains hidden or 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 was generated by Nexus Schema | |
### Do not make changes to this file directly | |
type Post { | |
body: String | |
id: Int | |
published: Boolean | |
title: String | |
} | |
type Query { | |
drafts: [Post]! | |
} |
This file contains hidden or 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
import { makeSchema } from "nexus"; | |
import path from "path"; | |
import * as types from "./graphql"; | |
const schema = makeSchema({ | |
types, | |
outputs: { | |
schema: path.join(process.cwd(), 'generated/schema.graphql'), | |
typegen: path.join(process.cwd(), 'generated/nexus-typegen.ts'), | |
} | |
}) | |
export default schema |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment