Skip to content

Instantly share code, notes, and snippets.

@tatsuyasusukida
Last active September 6, 2022 00:44
Show Gist options
  • Save tatsuyasusukida/0daa727b457e1e6d83814293905e1abc to your computer and use it in GitHub Desktop.
Save tatsuyasusukida/0daa727b457e1e6d83814293905e1abc to your computer and use it in GitHub Desktop.
Next.js + Apollo GraphQL + Nexus.js
import { ApolloServer } from 'apollo-server-micro'
import type { 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,
},
}
export * from './post'
/**
* 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 {
}
}
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 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]!
}
import { makeSchema } from "nexus";
import path from "path";
import * as types from './graphql';
const schema = makeSchema({
types,
outputs: {
typegen: path.join(process.cwd(), 'generated/nexus-typegen.ts'),
schema: path.join(process.cwd(), 'generated/schema.graphql'),
},
})
export default schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment