Skip to content

Commit eeaced0

Browse files
ardatangithub-actions[bot]saihaj
authored
Don't accept an object of typeDefs and resolvers (#1753)
* Don't accept an object of typeDefs and resolvers * chore(dependencies): updated changesets for modified dependencies * Update .changeset/fifty-elephants-provide.md Co-authored-by: Saihajpreet Singh <[email protected]> * Update changeset * Rebase * chore(dependencies): updated changesets for modified dependencies Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent e1596e3 commit eeaced0

File tree

18 files changed

+93
-156
lines changed

18 files changed

+93
-156
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-yoga': major
3+
---
4+
5+
`schema` no longer accepts an object of `typeDefs` and `resolvers` but instead you can use `createSchema` to create a GraphQL schema.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"graphql-yoga": patch
3+
---
4+
5+
dependencies updates:
6+
7+
- Removed dependency [`@graphql-tools/utils@^8.8.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/null) (from `dependencies`)

examples/defer-stream/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createYoga } from 'graphql-yoga'
1+
import { createSchema, createYoga } from 'graphql-yoga'
22
import { createServer } from 'http'
33

44
const wait = (time: number) =>
@@ -84,10 +84,10 @@ const resolvers = {
8484
}
8585

8686
const yoga = createYoga({
87-
schema: {
87+
schema: createSchema({
8888
typeDefs,
8989
resolvers,
90-
},
90+
}),
9191
graphiql: {
9292
defaultQuery: /* GraphQL */ `
9393
# Slow alphabet

examples/graphql-armor/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createYoga } from 'graphql-yoga'
1+
import { createSchema, createYoga } from 'graphql-yoga'
22
import { EnvelopArmor } from '@escape.tech/graphql-armor'
33
import { createServer } from 'http'
44

@@ -18,7 +18,7 @@ const booksStore = [
1818

1919
const yoga = createYoga({
2020
plugins: [...enhancements.plugins],
21-
schema: {
21+
schema: createSchema({
2222
typeDefs: /* GraphQL */ `
2323
type Book {
2424
title: String
@@ -33,7 +33,7 @@ const yoga = createYoga({
3333
books: () => booksStore,
3434
},
3535
},
36-
},
36+
}),
3737
})
3838

3939
const server = createServer(yoga)

examples/hello-world/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ The server returns the following response:
6363
This is what the [implementation](./index.js) looks like:
6464

6565
```js
66-
import { createServer } from 'http'
66+
import { createServer, createSchema } from 'http'
6767
import { createYoga } from 'graphql-yoga'
6868
// ... or using `require()`
69-
// const { createServer } = require('graphql-yoga')
69+
// const { createServer, createSchema } = require('graphql-yoga')
7070

71-
const typeDefs = `
71+
const typeDefs = /* GraphQL */ `
7272
type Query {
7373
hello(name: String): String!
7474
}
@@ -81,10 +81,10 @@ const resolvers = {
8181
}
8282

8383
const yoga = createYoga({
84-
schema: {
84+
schema: createSchema({
8585
typeDefs,
8686
resolvers,
87-
},
87+
}),
8888
})
8989

9090
const server = createServer(yoga)

packages/graphql-yoga/__tests__/http-extensions.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('GraphQLError.extensions.http', () => {
4242

4343
it('picks the highest status code and headers for GraphQLErrors thrown within multiple resolvers', async () => {
4444
const yoga = createYoga({
45-
schema: {
45+
schema: createSchema({
4646
typeDefs: /* GraphQL */ `
4747
type Query {
4848
a: String
@@ -71,7 +71,7 @@ describe('GraphQLError.extensions.http', () => {
7171
},
7272
},
7373
},
74-
},
74+
}),
7575
})
7676

7777
const response = await yoga.fetch('http://yoga/graphql', {
@@ -103,7 +103,7 @@ describe('GraphQLError.extensions.http', () => {
103103

104104
it('picks the header of the last GraphQLError when multiple errors are thrown within multiple resolvers', async () => {
105105
const yoga = createYoga({
106-
schema: {
106+
schema: createSchema({
107107
typeDefs: /* GraphQL */ `
108108
type Query {
109109
a: String
@@ -136,7 +136,7 @@ describe('GraphQLError.extensions.http', () => {
136136
},
137137
},
138138
},
139-
},
139+
}),
140140
})
141141

142142
let response = await yoga.fetch('http://yoga/graphql', {
@@ -158,7 +158,7 @@ describe('GraphQLError.extensions.http', () => {
158158

159159
it('should not contain the http extensions in response result', async () => {
160160
const yoga = createYoga({
161-
schema: {
161+
schema: createSchema({
162162
typeDefs: /* GraphQL */ `
163163
type Query {
164164
a: String
@@ -180,7 +180,7 @@ describe('GraphQLError.extensions.http', () => {
180180
},
181181
},
182182
},
183-
},
183+
}),
184184
})
185185

186186
let response = await yoga.fetch('http://yoga/graphql', {
@@ -197,13 +197,13 @@ describe('GraphQLError.extensions.http', () => {
197197

198198
it('should respect http extensions status consistently on parsing fail', async () => {
199199
const yoga = createYoga({
200-
schema: {
200+
schema: createSchema({
201201
typeDefs: /* GraphQL */ `
202202
type Query {
203203
_: String
204204
}
205205
`,
206-
},
206+
}),
207207
})
208208

209209
let response = await yoga.fetch('http://yoga/graphql', {
@@ -223,13 +223,13 @@ describe('GraphQLError.extensions.http', () => {
223223

224224
it('should respect http extensions status consistently on validation fail', async () => {
225225
const yoga = createYoga({
226-
schema: {
226+
schema: createSchema({
227227
typeDefs: /* GraphQL */ `
228228
type Query {
229229
_: String
230230
}
231231
`,
232-
},
232+
}),
233233
})
234234

235235
let response = await yoga.fetch('http://yoga/graphql', {
@@ -249,13 +249,13 @@ describe('GraphQLError.extensions.http', () => {
249249

250250
it('should respond with status 500 when error without http extension is thrown', async () => {
251251
const yoga = createYoga({
252-
schema: {
252+
schema: createSchema({
253253
typeDefs: /* GraphQL */ `
254254
type Query {
255255
_: String
256256
}
257257
`,
258-
},
258+
}),
259259
context: () => {
260260
throw new GraphQLError('No http status extension', {
261261
extensions: { http: { headers: { 'x-foo': 'bar' } } },

packages/graphql-yoga/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@envelop/validation-cache": "^4.6.0",
6161
"@graphql-typed-document-node/core": "^3.1.1",
6262
"@graphql-tools/schema": "^9.0.0",
63-
"@graphql-tools/utils": "^8.8.0",
6463
"@graphql-yoga/subscription": "^2.2.2",
6564
"@whatwg-node/fetch": "0.4.4",
6665
"@whatwg-node/server": "0.4.5",

packages/graphql-yoga/src/plugins/useSchema.ts

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,19 @@
11
import { PromiseOrValue } from '@envelop/core'
2-
import { makeExecutableSchema } from '@graphql-tools/schema'
3-
import { IResolvers, TypeSource } from '@graphql-tools/utils'
42
import { GraphQLError, GraphQLSchema, isSchema } from 'graphql'
53
import { GraphQLSchemaWithContext, YogaInitialContext } from '../types'
64
import { Plugin } from './types'
75

8-
// TODO: Will be removed later
9-
type TypeDefsAndResolvers<TContext, TRootValue = {}> = {
10-
typeDefs: TypeSource
11-
resolvers?:
12-
| IResolvers<TRootValue, TContext>
13-
| Array<IResolvers<TRootValue, TContext>>
14-
}
15-
16-
export type YogaSchemaDefinition<TContext, TRootValue> =
17-
| TypeDefsAndResolvers<TContext, TRootValue>
6+
export type YogaSchemaDefinition<TContext> =
187
| PromiseOrValue<GraphQLSchemaWithContext<TContext>>
198
| ((request: Request) => PromiseOrValue<GraphQLSchemaWithContext<TContext>>)
209

21-
// Will be moved to a seperate export later
22-
export function getDefaultSchema() {
23-
return makeExecutableSchema({
24-
typeDefs: /* GraphQL */ `
25-
"""
26-
Greetings from GraphQL Yoga!
27-
"""
28-
type Query {
29-
greetings: String
30-
}
31-
type Subscription {
32-
"""
33-
Current Time
34-
"""
35-
time: String
36-
}
37-
`,
38-
resolvers: {
39-
Query: {
40-
greetings: () =>
41-
'This is the `greetings` field of the root `Query` type',
42-
},
43-
Subscription: {
44-
time: {
45-
async *subscribe() {
46-
while (true) {
47-
yield { time: new Date().toISOString() }
48-
await new Promise((resolve) => setTimeout(resolve, 1000))
49-
}
50-
},
51-
},
52-
},
53-
},
54-
})
55-
}
56-
5710
export const useSchema = <
5811
TContext extends YogaInitialContext = YogaInitialContext,
59-
TRootValue = {},
6012
>(
61-
schemaDef?: YogaSchemaDefinition<TContext, TRootValue>,
13+
schemaDef?: YogaSchemaDefinition<TContext>,
6214
): Plugin<TContext> => {
6315
if (schemaDef == null) {
64-
const schema = getDefaultSchema()
65-
return {
66-
onPluginInit({ setSchema }) {
67-
setSchema(schema)
68-
},
69-
}
70-
}
71-
if ('typeDefs' in schemaDef) {
72-
const schema = makeExecutableSchema(schemaDef)
73-
return {
74-
onPluginInit({ setSchema }) {
75-
setSchema(schema)
76-
},
77-
}
16+
return {}
7817
}
7918
if (isSchema(schemaDef)) {
8019
return {

packages/graphql-yoga/src/server.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ import { useLimitBatching } from './plugins/requestValidation/useLimitBatching.j
8181
export type YogaServerOptions<
8282
TServerContext extends Record<string, any>,
8383
TUserContext extends Record<string, any>,
84-
TRootValue,
8584
> = {
8685
/**
8786
* Enable/disable logging or provide a custom logger.
@@ -147,8 +146,7 @@ export type YogaServerOptions<
147146
renderGraphiQL?: (options?: GraphiQLOptions) => PromiseOrValue<BodyInit>
148147

149148
schema?: YogaSchemaDefinition<
150-
TUserContext & TServerContext & YogaInitialContext,
151-
TRootValue
149+
TUserContext & TServerContext & YogaInitialContext
152150
>
153151

154152
/**
@@ -199,7 +197,6 @@ export type BatchingOptions =
199197
export class YogaServer<
200198
TServerContext extends Record<string, any>,
201199
TUserContext extends Record<string, any>,
202-
TRootValue,
203200
> {
204201
/**
205202
* Instance of envelop
@@ -221,9 +218,7 @@ export class YogaServer<
221218
private maskedErrorsOpts: YogaMaskedErrorOpts | null
222219
private id: string
223220

224-
constructor(
225-
options?: YogaServerOptions<TServerContext, TUserContext, TRootValue>,
226-
) {
221+
constructor(options?: YogaServerOptions<TServerContext, TUserContext>) {
227222
this.id = options?.id ?? 'yoga'
228223
this.fetchAPI =
229224
options?.fetchAPI ??
@@ -662,21 +657,14 @@ export class YogaServer<
662657
export type YogaServerInstance<
663658
TServerContext extends Record<string, any>,
664659
TUserContext extends Record<string, any>,
665-
TRootValue extends Record<string, any>,
666-
> = ServerAdapter<
667-
TServerContext,
668-
YogaServer<TServerContext, TUserContext, TRootValue>
669-
>
660+
> = ServerAdapter<TServerContext, YogaServer<TServerContext, TUserContext>>
670661

671662
export function createYoga<
672663
TServerContext extends Record<string, any> = {},
673664
TUserContext extends Record<string, any> = {},
674-
TRootValue extends Record<string, any> = {},
675665
>(
676-
options: YogaServerOptions<TServerContext, TUserContext, TRootValue>,
677-
): YogaServerInstance<TServerContext, TUserContext, TRootValue> {
678-
const server = new YogaServer<TServerContext, TUserContext, TRootValue>(
679-
options,
680-
)
666+
options: YogaServerOptions<TServerContext, TUserContext>,
667+
): YogaServerInstance<TServerContext, TUserContext> {
668+
const server = new YogaServer<TServerContext, TUserContext>(options)
681669
return createServerAdapter(server, server.fetchAPI.Request)
682670
}

website/src/pages/v3/features/envelop-plugins.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ The following example adds [GraphQL JIT](https://github.com/zalando-incubator/gr
1212

1313
```ts
1414
import { useGraphQlJit } from '@envelop/graphql-jit'
15-
import { createYoga } from 'graphql-yoga'
15+
import { createYoga, createSchema } from 'graphql-yoga'
1616

1717
// Provide your schema
1818
const yoga = createYoga({
19-
schema: {
19+
schema: createSchema({
2020
typeDefs: /* GraphQL */ `
2121
type Query {
2222
greetings: String!
@@ -27,7 +27,7 @@ const yoga = createYoga({
2727
greetings: () => 'Hello World!',
2828
},
2929
},
30-
},
30+
}),
3131
plugins: [useGraphQlJit()],
3232
})
3333

0 commit comments

Comments
 (0)