2019-08-16 05:48:42 +08:00
|
|
|
import {
|
2020-12-07 07:52:04 +08:00
|
|
|
arg,
|
2022-05-16 04:59:05 +08:00
|
|
|
booleanArg,
|
2020-12-01 04:14:01 +08:00
|
|
|
connectionPlugin,
|
2022-05-16 04:59:05 +08:00
|
|
|
core,
|
2020-12-01 04:14:01 +08:00
|
|
|
declarativeWrappingPlugin,
|
2022-05-16 04:59:05 +08:00
|
|
|
directive,
|
2020-04-07 20:19:45 +08:00
|
|
|
dynamicInputMethod,
|
|
|
|
|
dynamicOutputMethod,
|
|
|
|
|
dynamicOutputProperty,
|
2020-12-07 07:52:04 +08:00
|
|
|
enumType,
|
2020-04-07 20:19:45 +08:00
|
|
|
extendType,
|
2019-08-16 05:48:42 +08:00
|
|
|
idArg,
|
2020-04-07 20:19:45 +08:00
|
|
|
inputObjectType,
|
2020-11-14 03:19:15 +08:00
|
|
|
interfaceType,
|
2019-08-16 05:48:42 +08:00
|
|
|
mutationType,
|
2021-02-01 12:04:03 +08:00
|
|
|
nonNull,
|
2020-04-07 20:19:45 +08:00
|
|
|
objectType,
|
|
|
|
|
queryType,
|
2020-12-01 04:14:01 +08:00
|
|
|
scalarType,
|
2019-08-16 05:48:42 +08:00
|
|
|
stringArg,
|
2020-12-03 00:53:55 +08:00
|
|
|
subscriptionField,
|
2020-10-20 23:13:08 +08:00
|
|
|
subscriptionType,
|
2022-05-16 04:59:05 +08:00
|
|
|
addDirective,
|
2020-10-23 04:08:00 +08:00
|
|
|
} from '../../../src'
|
|
|
|
|
import { mockStream } from '../../__helpers'
|
2020-11-19 00:59:59 +08:00
|
|
|
import './__typegen'
|
2019-09-24 04:35:34 +08:00
|
|
|
|
2022-06-29 01:21:58 +08:00
|
|
|
export const Node = interfaceType({
|
|
|
|
|
name: 'Node',
|
|
|
|
|
definition(t) {
|
|
|
|
|
t.nonNull.id('id', {
|
|
|
|
|
resolve: (source, args, ctx, info) => `${info.parentType.name}:${source.id}`,
|
|
|
|
|
sourceType: 'number',
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
resolveType(obj: any) {
|
|
|
|
|
return obj.__typename
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2022-05-16 04:59:05 +08:00
|
|
|
export const typeDirective = directive({
|
|
|
|
|
name: 'TestTypeDirective',
|
|
|
|
|
locations: ['OBJECT', 'INTERFACE', 'ENUM', 'SCALAR'],
|
|
|
|
|
description: 'Testing type directives',
|
|
|
|
|
args: {
|
|
|
|
|
bool: booleanArg(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const fieldDirective = directive({
|
|
|
|
|
name: 'TestFieldDirective',
|
|
|
|
|
locations: ['FIELD_DEFINITION', 'INPUT_FIELD_DEFINITION'],
|
|
|
|
|
description: 'Testing the object directive',
|
|
|
|
|
args: {
|
|
|
|
|
bool: booleanArg(),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const testAllDirective = directive({
|
|
|
|
|
name: 'TestAllDirective',
|
|
|
|
|
locations: core.SchemaDirectiveLocation,
|
|
|
|
|
description: 'Testing directive in all positions',
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const testRequiredArgDirective = directive({
|
|
|
|
|
name: 'TestRequiredArgDirective',
|
|
|
|
|
locations: ['OBJECT'],
|
|
|
|
|
description: 'Testing with required arg',
|
|
|
|
|
args: {
|
|
|
|
|
bool: nonNull(booleanArg()),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const testRepeatableDirective = directive({
|
|
|
|
|
name: 'TestRepeatableDirective',
|
|
|
|
|
locations: core.SchemaDirectiveLocation,
|
|
|
|
|
description: 'Testing a repeatable directive',
|
|
|
|
|
isRepeatable: true,
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-25 00:40:55 +08:00
|
|
|
export const scalar = scalarType({
|
|
|
|
|
name: 'MyCustomScalar',
|
|
|
|
|
description: 'No-Op scalar for testing purposes only',
|
|
|
|
|
asNexusMethod: 'myCustomScalar',
|
2020-11-28 03:22:48 +08:00
|
|
|
serialize() {},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [testAllDirective],
|
2020-11-25 00:40:55 +08:00
|
|
|
})
|
|
|
|
|
|
2019-09-24 04:35:34 +08:00
|
|
|
export const query = queryType({
|
|
|
|
|
definition(t) {
|
2020-11-05 01:03:29 +08:00
|
|
|
t.string('foo', { resolve: () => 'bar' })
|
2020-11-25 00:40:55 +08:00
|
|
|
t.myCustomScalar('customScalar')
|
2019-09-24 04:35:34 +08:00
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [testRepeatableDirective, testRepeatableDirective],
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
|
|
|
|
|
const mockData = {
|
2020-07-02 11:42:06 +08:00
|
|
|
posts: [{ title: '', body: '' }],
|
|
|
|
|
user: { firstName: '', lastName: '' },
|
|
|
|
|
}
|
2019-08-16 05:48:42 +08:00
|
|
|
|
2020-11-14 03:19:15 +08:00
|
|
|
export const I = interfaceType({
|
|
|
|
|
name: 'I',
|
|
|
|
|
resolveType() {
|
|
|
|
|
return 'OfI'
|
|
|
|
|
},
|
|
|
|
|
definition(t) {
|
2020-12-07 22:10:15 +08:00
|
|
|
t.string('hello', {
|
|
|
|
|
extensions: {
|
|
|
|
|
extensionAdditionFromFieldConfig: true,
|
|
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [fieldDirective({ bool: true })],
|
2020-12-07 22:10:15 +08:00
|
|
|
})
|
2020-11-14 03:19:15 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const i = objectType({
|
|
|
|
|
name: 'OfI',
|
|
|
|
|
definition(t) {
|
|
|
|
|
t.implements('I')
|
|
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [typeDirective],
|
2020-11-14 03:19:15 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const i2 = objectType({
|
|
|
|
|
name: 'OfI2',
|
2020-12-07 22:10:15 +08:00
|
|
|
extensions: {
|
|
|
|
|
extensionAdditionFromTypeConfig: true,
|
|
|
|
|
},
|
2020-11-14 03:19:15 +08:00
|
|
|
definition(t) {
|
2022-06-29 01:21:58 +08:00
|
|
|
t.implements(Node)
|
2020-11-14 03:19:15 +08:00
|
|
|
t.implements('I')
|
2020-12-07 22:10:15 +08:00
|
|
|
t.modify('hello', {
|
|
|
|
|
extensions: {
|
|
|
|
|
extensionAdditionFromModifyMethod: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-06-29 01:21:58 +08:00
|
|
|
t.string('composite', {
|
|
|
|
|
resolve: (source) => `${source.fieldA} ${source.fieldB}`,
|
|
|
|
|
sourceType: [
|
|
|
|
|
{ name: 'fieldA', type: 'string' },
|
|
|
|
|
{ name: 'fieldB', type: 'string' },
|
|
|
|
|
],
|
|
|
|
|
})
|
2020-11-14 03:19:15 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2019-09-01 21:32:57 +08:00
|
|
|
export const dom = dynamicOutputMethod({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'title',
|
|
|
|
|
typeDefinition: '(options: { escape: boolean }): void',
|
2020-11-25 00:40:55 +08:00
|
|
|
typeDescription: 'Title of the page, optionally escaped',
|
2019-09-01 21:32:57 +08:00
|
|
|
factory: ({ typeDef: t }) => {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.string('title')
|
2019-09-01 21:32:57 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-09-01 21:32:57 +08:00
|
|
|
|
|
|
|
|
export const dim = dynamicInputMethod({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'title',
|
2019-09-01 21:32:57 +08:00
|
|
|
factory: ({ typeDef: t }) => {
|
2020-11-19 00:59:59 +08:00
|
|
|
t.string('title')
|
2019-09-01 21:32:57 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-09-01 21:32:57 +08:00
|
|
|
|
|
|
|
|
export const dop = dynamicOutputProperty({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'body',
|
2020-11-25 00:40:55 +08:00
|
|
|
typeDescription: 'adds a body (weirdly, as a getter)',
|
2019-09-01 21:32:57 +08:00
|
|
|
factory: ({ typeDef: t }) => {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.string('body')
|
2019-09-01 21:32:57 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-09-01 21:32:57 +08:00
|
|
|
|
2019-08-16 05:48:42 +08:00
|
|
|
export const PostSearchInput = inputObjectType({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'PostSearchInput',
|
2019-08-16 05:48:42 +08:00
|
|
|
definition(t) {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.title()
|
2020-11-19 00:59:59 +08:00
|
|
|
t.string('body')
|
2019-08-16 05:48:42 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
|
2020-12-07 07:52:04 +08:00
|
|
|
export const someArg = arg({
|
|
|
|
|
type: inputObjectType({
|
|
|
|
|
name: 'Something',
|
|
|
|
|
definition(t) {
|
|
|
|
|
t.nonNull.int('id')
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
default: { id: 1 },
|
|
|
|
|
})
|
|
|
|
|
|
2019-08-16 05:48:42 +08:00
|
|
|
export const Post = objectType({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'Post',
|
2019-08-16 05:48:42 +08:00
|
|
|
definition(t) {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.title({ escape: true })
|
2019-09-01 21:32:57 +08:00
|
|
|
// tslint:disable-next-line: no-unused-expression
|
2020-07-02 11:42:06 +08:00
|
|
|
t.body
|
2019-08-16 05:48:42 +08:00
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [testRequiredArgDirective({ bool: true })],
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
|
|
|
|
|
export const User = objectType({
|
2020-07-02 11:42:06 +08:00
|
|
|
name: 'User',
|
2019-08-16 05:48:42 +08:00
|
|
|
definition(t) {
|
2022-06-29 01:21:58 +08:00
|
|
|
t.string('firstName', {
|
|
|
|
|
sourceType: 'string',
|
|
|
|
|
})
|
|
|
|
|
t.string('lastName', {
|
|
|
|
|
sourceType: 'string',
|
|
|
|
|
})
|
2020-11-26 06:43:49 +08:00
|
|
|
t.connectionField('posts', {
|
|
|
|
|
type: Post,
|
|
|
|
|
nodes() {
|
|
|
|
|
return mockData.posts
|
|
|
|
|
},
|
2021-02-01 12:04:03 +08:00
|
|
|
totalCount(root) {
|
|
|
|
|
if (!root.firstName) {
|
|
|
|
|
// root.firstName should be the source value here
|
|
|
|
|
throw new Error()
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
},
|
2020-11-26 06:43:49 +08:00
|
|
|
edgeFields: {
|
|
|
|
|
delta(root, args, ctx) {
|
|
|
|
|
if (root.cursor) {
|
|
|
|
|
// Cursor should be defined here
|
|
|
|
|
}
|
2020-12-08 06:02:36 +08:00
|
|
|
if (args.format === 'ms') {
|
|
|
|
|
return '5ms'
|
|
|
|
|
}
|
|
|
|
|
return '0.005s'
|
2020-11-26 06:43:49 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-06-29 01:21:58 +08:00
|
|
|
t.string('telephone', {
|
|
|
|
|
sourceType: { type: 'string', optional: true },
|
|
|
|
|
resolve: (source) => (source.telephone ? `+1 ${source.telephone}` : null),
|
|
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
|
|
|
|
|
export const Query = extendType({
|
2020-07-02 11:42:06 +08:00
|
|
|
type: 'Query',
|
2019-08-16 05:48:42 +08:00
|
|
|
definition(t) {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.list.field('searchPosts', {
|
|
|
|
|
type: 'Post',
|
2019-08-16 05:48:42 +08:00
|
|
|
args: { input: PostSearchInput },
|
|
|
|
|
resolve: () => mockData.posts,
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
|
|
|
|
t.field('user', {
|
|
|
|
|
type: 'User',
|
2020-12-07 07:52:04 +08:00
|
|
|
args: {
|
|
|
|
|
id: idArg(),
|
|
|
|
|
status: enumType({
|
|
|
|
|
name: 'UserStatus',
|
|
|
|
|
members: [
|
|
|
|
|
{ name: 'ACTIVE', value: 'active' },
|
|
|
|
|
{ name: 'PENDING', value: 'pending' },
|
|
|
|
|
],
|
2022-05-16 04:59:05 +08:00
|
|
|
}).asArg({ default: 'active', directives: [testAllDirective] }),
|
2020-12-07 07:52:04 +08:00
|
|
|
},
|
2019-08-16 05:48:42 +08:00
|
|
|
resolve: () => mockData.user,
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
|
|
|
|
|
export const Mutation = mutationType({
|
|
|
|
|
definition(t) {
|
2020-07-02 11:42:06 +08:00
|
|
|
t.field('createUser', {
|
|
|
|
|
type: 'User',
|
2019-08-16 05:48:42 +08:00
|
|
|
args: { firstName: stringArg(), lastName: stringArg() },
|
2020-07-02 11:42:06 +08:00
|
|
|
resolve: (_root) => ({ firstName: '', lastName: '' }),
|
|
|
|
|
})
|
2019-08-16 05:48:42 +08:00
|
|
|
},
|
2020-07-02 11:42:06 +08:00
|
|
|
})
|
2020-10-20 23:13:08 +08:00
|
|
|
|
2020-12-01 04:14:01 +08:00
|
|
|
export const Subscription2 = extendType({
|
|
|
|
|
type: 'Subscription',
|
|
|
|
|
definition(t) {
|
2020-12-03 00:53:55 +08:00
|
|
|
t.boolean('someBooleanFromExtendType', {
|
2020-12-01 04:14:01 +08:00
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, true, (b) => b)
|
|
|
|
|
},
|
|
|
|
|
resolve: (event: boolean) => {
|
|
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2020-12-03 00:53:55 +08:00
|
|
|
export const Subscription3 = subscriptionField((t) => {
|
|
|
|
|
t.boolean('someBooleanFromSubscriptionField', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, true, (b) => b)
|
|
|
|
|
},
|
|
|
|
|
resolve: (event: boolean) => {
|
|
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2020-10-20 23:13:08 +08:00
|
|
|
export const Subscription = subscriptionType({
|
|
|
|
|
definition(t) {
|
|
|
|
|
// lists
|
|
|
|
|
t.list.field('someFields', {
|
|
|
|
|
type: 'Int',
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 0, (int) => int - 1)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: number) => {
|
|
|
|
|
return [event]
|
2020-10-20 23:13:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
t.list.int('someInts', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 0, (int) => int + 1)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: number) => {
|
|
|
|
|
return [event]
|
2020-10-20 23:13:08 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
// singular
|
|
|
|
|
t.field('someField', {
|
|
|
|
|
type: 'Int',
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 0, (int) => int - 1)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: number) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
t.int('someInt', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 0, (int) => int + 1)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: number) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [testAllDirective],
|
2020-10-20 23:13:08 +08:00
|
|
|
})
|
|
|
|
|
t.string('someString', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, '', (str) => str + '!')
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: string) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
2022-05-16 04:59:05 +08:00
|
|
|
directives: [addDirective('TestFieldDirective', { bool: true })],
|
2020-10-20 23:13:08 +08:00
|
|
|
})
|
|
|
|
|
t.float('someFloat', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 0.5, (f) => f)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: number) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
t.boolean('someBoolean', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, true, (b) => b)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: boolean) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
t.id('someID', {
|
|
|
|
|
subscribe() {
|
|
|
|
|
return mockStream(10, 'abc', (id) => id)
|
|
|
|
|
},
|
2020-11-23 23:41:03 +08:00
|
|
|
resolve: (event: string) => {
|
2020-10-20 23:13:08 +08:00
|
|
|
return event
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
})
|
2020-11-26 06:43:49 +08:00
|
|
|
|
|
|
|
|
export const plugins = [
|
|
|
|
|
declarativeWrappingPlugin({ disable: true }),
|
|
|
|
|
connectionPlugin({
|
|
|
|
|
extendEdge: {
|
|
|
|
|
delta: {
|
2020-12-08 06:02:36 +08:00
|
|
|
type: 'String',
|
|
|
|
|
args: {
|
|
|
|
|
format: 'String',
|
|
|
|
|
},
|
2020-11-26 06:43:49 +08:00
|
|
|
},
|
|
|
|
|
},
|
2021-02-01 12:04:03 +08:00
|
|
|
extendConnection: {
|
|
|
|
|
totalCount: {
|
|
|
|
|
type: nonNull('Int'),
|
|
|
|
|
},
|
|
|
|
|
},
|
2020-11-26 06:43:49 +08:00
|
|
|
}),
|
|
|
|
|
]
|