-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add docs and tests for removing fields
- Loading branch information
Michael Hayes
committed
May 12, 2021
1 parent
3251227
commit a3aa90e
Showing
10 changed files
with
225 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 contains 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 contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`plugin example generates expected schema 1`] = ` | ||
"type Query { | ||
args(input: TestInput, notRemoved: Boolean, testEnum: TestEnum): Boolean! | ||
notRemoved: Boolean! | ||
} | ||
enum TestEnum { | ||
DONT_REMOVE | ||
} | ||
input TestInput { | ||
notRemoved: Boolean | ||
} | ||
" | ||
`; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import SchemaBuilder from '../../../src'; | ||
import TestPlugin from './plugin'; | ||
|
||
const builder = new SchemaBuilder({ | ||
plugins: [TestPlugin], | ||
}); | ||
|
||
export default builder; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { FieldNullability, InputFieldMap, SchemaTypes, TypeParam } from '@giraphql/core'; | ||
import { GiraphQLTestPlugin } from './plugin'; | ||
|
||
declare global { | ||
export namespace GiraphQLSchemaTypes { | ||
export interface Plugins<Types extends SchemaTypes> { | ||
test: GiraphQLTestPlugin<Types>; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import builder from './builder'; | ||
|
||
const TestEnum = builder.enumType('TestEnum', { | ||
values: { | ||
REMOVE: { | ||
value: 'removeMe', | ||
}, | ||
DONT_REMOVE: { | ||
value: 'dontRemoveMe', | ||
}, | ||
}, | ||
}); | ||
|
||
const TestInput = builder.inputType('TestInput', { | ||
fields: (t) => ({ | ||
removeMe: t.boolean({}), | ||
notRemoved: t.boolean({}), | ||
}), | ||
}); | ||
|
||
builder.queryType({ | ||
fields: (t) => ({ | ||
notRemoved: t.boolean({ | ||
resolve: () => true, | ||
}), | ||
removeMe: t.boolean({ | ||
resolve: () => true, | ||
}), | ||
args: t.boolean({ | ||
args: { | ||
input: t.arg({ | ||
type: TestInput, | ||
}), | ||
removeMe: t.arg.boolean({}), | ||
notRemoved: t.arg.boolean({}), | ||
testEnum: t.arg({ type: TestEnum }), | ||
}, | ||
resolve: () => true, | ||
}), | ||
}), | ||
}); | ||
|
||
const schema = builder.toSchema({}); | ||
|
||
export default schema; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { GraphQLFieldResolver, GraphQLSchema, GraphQLTypeResolver } from 'graphql'; | ||
import SchemaBuilder, { | ||
BasePlugin, | ||
GiraphQLEnumValueConfig, | ||
GiraphQLInputFieldConfig, | ||
GiraphQLInterfaceTypeConfig, | ||
GiraphQLOutputFieldConfig, | ||
GiraphQLTypeConfig, | ||
GiraphQLUnionTypeConfig, | ||
SchemaTypes, | ||
} from '../../../src'; | ||
|
||
const pluginName = 'test' as const; | ||
|
||
export default pluginName; | ||
|
||
export class GiraphQLTestPlugin<Types extends SchemaTypes> extends BasePlugin<Types> { | ||
onTypeConfig(typeConfig: GiraphQLTypeConfig) { | ||
return typeConfig; | ||
} | ||
|
||
onOutputFieldConfig(fieldConfig: GiraphQLOutputFieldConfig<Types>) { | ||
if (fieldConfig.name === 'removeMe') { | ||
return null; | ||
} | ||
|
||
return fieldConfig; | ||
} | ||
|
||
onInputFieldConfig(fieldConfig: GiraphQLInputFieldConfig<Types>) { | ||
if (fieldConfig.name === 'removeMe') { | ||
return null; | ||
} | ||
|
||
return fieldConfig; | ||
} | ||
|
||
onEnumValueConfig(valueConfig: GiraphQLEnumValueConfig<Types>) { | ||
if (valueConfig.value === 'removeMe') { | ||
return null; | ||
} | ||
|
||
return valueConfig; | ||
} | ||
|
||
beforeBuild() {} | ||
|
||
afterBuild(schema: GraphQLSchema): GraphQLSchema { | ||
return schema; | ||
} | ||
|
||
wrapResolve( | ||
resolver: GraphQLFieldResolver<unknown, Types['Context'], object>, | ||
fieldConfig: GiraphQLOutputFieldConfig<Types>, | ||
): GraphQLFieldResolver<unknown, Types['Context'], object> { | ||
return (parent, args, context, info) => resolver(parent, args, context, info) as unknown; | ||
} | ||
|
||
wrapSubscribe( | ||
subscribe: GraphQLFieldResolver<unknown, Types['Context'], object> | undefined, | ||
fieldConfig: GiraphQLOutputFieldConfig<Types>, | ||
) { | ||
return subscribe; | ||
} | ||
|
||
wrapResolveType( | ||
resolveType: GraphQLTypeResolver<unknown, Types['Context']>, | ||
typeConfig: GiraphQLInterfaceTypeConfig | GiraphQLUnionTypeConfig, | ||
) { | ||
return resolveType; | ||
} | ||
} | ||
|
||
SchemaBuilder.registerPlugin(pluginName, GiraphQLTestPlugin); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApolloServer } from 'apollo-server'; | ||
import schema from '.'; | ||
|
||
const server = new ApolloServer({ schema }); | ||
|
||
server | ||
.listen(8000, (error: unknown) => { | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
console.log('🚀 Server started at http://127.0.0.1:8000'); | ||
}) | ||
.catch(console.error); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { lexicographicSortSchema, printSchema } from 'graphql'; | ||
import pluginSchema from './examples/test-plugin'; | ||
|
||
describe('plugin example', () => { | ||
it('generates expected schema', () => { | ||
expect(printSchema(lexicographicSortSchema(pluginSchema))).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains 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