Skip to content

Commit

Permalink
feat: export subscription and schema stuff not from the root package
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jul 26, 2022
1 parent 86b8d80 commit 779a6f6
Show file tree
Hide file tree
Showing 21 changed files with 209 additions and 128 deletions.
5 changes: 3 additions & 2 deletions examples/fastify/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createYoga } from 'graphql-yoga'
import fastify, { FastifyReply, FastifyRequest } from 'fastify'
import { createSchema } from 'graphql-yoga/schema'

export function buildApp(logging = true) {
const app = fastify({
Expand All @@ -15,7 +16,7 @@ export function buildApp(logging = true) {
req: FastifyRequest
reply: FastifyReply
}>({
schema: {
schema: createSchema({
typeDefs: /* GraphQL */ `
scalar File
Expand Down Expand Up @@ -53,7 +54,7 @@ export function buildApp(logging = true) {
},
},
},
},
}),
// Integrate Fastify Logger to Yoga
logging: {
debug: (...args) => args.forEach((arg) => app.log.debug(arg)),
Expand Down
28 changes: 21 additions & 7 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,32 @@
"default": "./dist/esm/index.js"
}
},
"./*": {
"./schema": {
"require": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/cjs/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/cjs/schema.js"
},
"import": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/esm/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/esm/schema.js"
},
"default": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/esm/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/esm/schema.js"
}
},
"./subscription": {
"require": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/cjs/subscription.js"
},
"import": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/esm/subscription.js"
},
"default": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/esm/subscription.js"
}
},
"./package.json": "./package.json"
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
console.warn(
`@graphql-yoga/common package has been deprecated and will no longer receive updates in the next major.`,
)
console.warn(`Please use 'graphql-yoga' npm package instead!'`)
import './warning.js'
export * from 'graphql-yoga'
2 changes: 2 additions & 0 deletions packages/common/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './warning.js'
export * from 'graphql-yoga/schema'
2 changes: 2 additions & 0 deletions packages/common/src/subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './warning.js'
export * from 'graphql-yoga/subscription'
4 changes: 4 additions & 0 deletions packages/common/src/warning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
console.warn(
`@graphql-yoga/common package has been deprecated and will no longer receive updates in the next major.`,
)
console.warn(`Please use 'graphql-yoga' npm package instead!'`)
17 changes: 9 additions & 8 deletions packages/graphql-yoga/__tests__/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '@whatwg-node/fetch'
import { ExecutionResult } from '@graphql-tools/utils'
import { ServerResponse, Server } from 'http'
import { createSchema } from '../src/schema.js'

describe('Disable Introspection with plugin', () => {
it('succeeds introspection query', async () => {
Expand Down Expand Up @@ -91,10 +92,10 @@ describe('Masked Error Option', () => {
},
}

const schema = {
const schema = createSchema({
typeDefs,
resolvers,
}
})

const initialEnv = process.env.NODE_ENV

Expand Down Expand Up @@ -770,7 +771,7 @@ describe('file uploads', () => {
const targetFilePath = path.join(os.tmpdir(), `${id}.png`)

const yoga = createYoga({
schema: {
schema: createSchema({
resolvers: {
Mutation: {
uploadFile: async (root, args) => {
Expand All @@ -792,7 +793,7 @@ describe('file uploads', () => {
uploadFile(file: File!): Boolean
}
`,
},
}),
logging: false,
})
const server = createServer(yoga)
Expand Down Expand Up @@ -864,7 +865,7 @@ it('should expose Node req and res objects in the context', async () => {
req: IncomingMessage
res: ServerResponse
}>({
schema: {
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
isNode: Boolean!
Expand All @@ -875,7 +876,7 @@ it('should expose Node req and res objects in the context', async () => {
isNode: (_, __, { req, res }) => !!req && !!res,
},
},
},
}),
logging: false,
})
const response = await request(yoga)
Expand Down Expand Up @@ -917,7 +918,7 @@ test('Subscription is closed properly', async () => {
}
const yoga = createYoga({
logging: false,
schema: {
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
_: Boolean
Expand All @@ -934,7 +935,7 @@ test('Subscription is closed properly', async () => {
},
},
},
},
}),
})
const server = createServer(yoga)
try {
Expand Down
28 changes: 21 additions & 7 deletions packages/graphql-yoga/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,32 @@
"default": "./dist/esm/index.js"
}
},
"./*": {
"./schema": {
"require": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/cjs/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/cjs/schema.js"
},
"import": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/esm/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/esm/schema.js"
},
"default": {
"types": "./dist/typings/*.d.ts",
"default": "./dist/esm/*.js"
"types": "./dist/typings/schema.d.ts",
"default": "./dist/esm/schema.js"
}
},
"./subscription": {
"require": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/cjs/subscription.js"
},
"import": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/esm/subscription.js"
},
"default": {
"types": "./dist/typings/subscription.d.ts",
"default": "./dist/esm/subscription.js"
}
},
"./package.json": "./package.json"
Expand Down
1 change: 0 additions & 1 deletion packages/graphql-yoga/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './logger.js'
export * from './server.js'

export * from '@envelop/core'
export * from '@graphql-yoga/subscription'
export { CORSOptions } from './plugins/useCORS.js'
export {
GraphiQLOptions,
Expand Down
48 changes: 48 additions & 0 deletions packages/graphql-yoga/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
IExecutableSchemaDefinition,
makeExecutableSchema,
} from '@graphql-tools/schema'
import { GraphQLSchemaWithContext, YogaInitialContext } from './types.js'

export function createSchema<TContext = YogaInitialContext>(
opts: IExecutableSchemaDefinition<TContext>,
): GraphQLSchemaWithContext<TContext> {
return makeExecutableSchema<TContext>(opts)
}

export function getDefaultSchema<
TContext,
>(): GraphQLSchemaWithContext<TContext> {
return createSchema<TContext>({
typeDefs: /* GraphQL */ `
"""
Greetings from GraphQL Yoga!
"""
type Query {
greetings: String
}
type Subscription {
"""
Current Time
"""
time: String
}
`,
resolvers: {
Query: {
greetings: () =>
'This is the `greetings` field of the root `Query` type',
},
Subscription: {
time: {
async *subscribe() {
while (true) {
yield { time: new Date().toISOString() }
await new Promise((resolve) => setTimeout(resolve, 1000))
}
},
},
},
},
})
}
Loading

0 comments on commit 779a6f6

Please sign in to comment.