def __init__(self, sdl=None, schema=None, serialize_callback=None): self.schema = schema or GraphQLSchema() if sdl: build_graphql_schema_from_sdl(sdl, schema=self.schema) self._serialize_callback = serialize_callback \ if serialize_callback else json.dumps self._parser = TartifletteRequestParser() self.schema.bake_schema()
def _preheat(schema_name: str) -> GraphQLSchema: schema_info = SchemaRegistry.find_schema_info(schema_name) schema = schema_info.get("inst", GraphQLSchema(name=schema_name)) sdl = schema_info["sdl"] build_graphql_schema_from_sdl(sdl, schema=schema) for object_ids in _SCHEMA_OBJECT_IDS: for obj in schema_info.get(object_ids, {}).values(): obj.bake(schema) schema_info["inst"] = schema return schema
def test_schema_validate_named_types(full_sdl, expected_error, expected_value): generated_schema = build_graphql_schema_from_sdl(full_sdl, schema=GraphQLSchema()) if expected_error: with pytest.raises(GraphQLSchemaError): generated_schema.validate_schema() else: assert generated_schema.validate_schema() == expected_value
def _preheat( schema_name: str, exclude_builtins_scalars: Optional[List[str]]) -> GraphQLSchema: schema_info = SchemaRegistry.find_schema_info(schema_name) schema = schema_info.get("inst", GraphQLSchema(name=schema_name)) sdl = schema_info["sdl"] build_graphql_schema_from_sdl(sdl, schema=schema) SchemaBakery._inject_default_object(schema_name, exclude_builtins_scalars) for object_ids in _SCHEMA_OBJECT_IDS: for obj in schema_info.get(object_ids, {}).values(): obj.bake(schema) schema_info["inst"] = schema return schema
def test_schema_get_resolver(): schema_sdl = """ schema { query: RootQuery } type Something { oneField: [Int] anotherField: [String] userInfo: UserInfo } type UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! smthg: Something } type RootQuery { defaultField: Int testField: Something } """ generated_schema = build_graphql_schema_from_sdl(schema_sdl, schema=GraphQLSchema()) def a(): pass def b(): pass def c(): pass generated_schema.get_field_by_name("RootQuery.defaultField").resolver = a generated_schema.get_field_by_name("Something.oneField").resolver = b generated_schema.get_field_by_name("Something.anotherField").resolver = c assert generated_schema.get_resolver("defaultField") == a assert generated_schema.get_resolver("RootQuery.defaultField") == a assert generated_schema.get_resolver( "RootQuery.testField.anotherField") == c assert generated_schema.get_resolver("testField.anotherField") == c assert generated_schema.get_resolver( "testField.userInfo.smthg.oneField") == b assert generated_schema.get_resolver( "RootQuery.testField.userInfo.smthg.oneField.unknownField") is None assert generated_schema.get_resolver( "RootQuery.testField.userInfo.oneField.unknownField") is None
def test_build_schema(): schema_sdl = """ schema @enable_cache { query: RootQuery mutation: RootMutation subscription: RootSubscription } scalar Date union Group = Foo | Bar | Baz interface Something { oneField: [Int] anotherField: [String] aLastOne: [[Date!]]! } input UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! } # directive @partner(goo: Anything) on ENUM_VALUE \"\"\" This is a docstring for the Test Object Type. \"\"\" type Test implements Unknown & Empty @enable_cache { \"\"\" This is a field description :D \"\"\" field(input: InputObject): String! @deprecated(reason: "Useless field") anotherField: [Int] @something( lst: ["about" "stuff"] obj: {some: [4, 8, 16], complex: {about: 19.4}, another: EnumVal} ) fieldWithDefaultValueArg(test: String = "default"): ID simpleField: Date } """ generated_schema = build_graphql_schema_from_sdl(schema_sdl, schema=GraphQLSchema()) expected_schema = GraphQLSchema() expected_schema.query_type = "RootQuery" expected_schema.mutation_type = "RootMutation" expected_schema.subscription_type = "RootSubscription" expected_schema.add_definition(GraphQLScalarType(name="Date")) expected_schema.add_definition(GraphQLUnionType(name="Group", gql_types=[ "Foo", "Bar", "Baz", ])) expected_schema.add_definition(GraphQLInterfaceType(name="Something", fields=OrderedDict( oneField=GraphQLField(name="oneField", gql_type=GraphQLList(gql_type="Int")), anotherField=GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="String")), aLastOne=GraphQLField(name="aLastOne", gql_type=GraphQLNonNull(gql_type=GraphQLList(gql_type=GraphQLList(gql_type=GraphQLNonNull(gql_type="Date"))))), ))) expected_schema.add_definition(GraphQLInputObjectType(name="UserInfo", fields=OrderedDict([ ('name', GraphQLArgument(name="name", gql_type="String")), ('dateOfBirth', GraphQLArgument(name="dateOfBirth", gql_type=GraphQLList(gql_type="Date"))), ('graphQLFan', GraphQLArgument(name="graphQLFan", gql_type=GraphQLNonNull(gql_type="Boolean"))), ]))) expected_schema.add_definition(GraphQLObjectType(name="Test", fields=OrderedDict([ ('field', GraphQLField(name="field", gql_type=GraphQLNonNull(gql_type="String"), arguments=OrderedDict( input=GraphQLArgument(name="input", gql_type="InputObject"), ))), ('anotherField', GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="Int"))), ('fieldWithDefaultValueArg', GraphQLField(name="fieldWithDefaultValueArg", gql_type="ID", arguments=OrderedDict( test=GraphQLArgument(name="test", gql_type="String", default_value="default"), ))), ('simpleField', GraphQLField(name="simpleField", gql_type="Date")), ]), interfaces=[ "Unknown", "Empty", ])) assert expected_schema == generated_schema assert len(generated_schema._gql_types) > 5 assert len(generated_schema._gql_types) == len(expected_schema._gql_types) # Only the default types should be in the schema assert len(DefaultGraphQLSchema._gql_types) == 5
def test_schema_object_get_field_name(): schema_sdl = """ schema @enable_cache { query: RootQuery mutation: RootMutation subscription: RootSubscription } scalar Date union Group = Foo | Bar | Baz interface Something { oneField: [Int] anotherField: [String] aLastOne: [[Date!]]! } input UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! } type RootQuery { defaultField: Int } # Query has been replaced by RootQuery as entrypoint type Query { nonDefaultField: String } \"\"\" This is a docstring for the Test Object Type. \"\"\" type Test implements Unknown & Empty @enable_cache { \"\"\" This is a field description :D \"\"\" field(input: InputObject): String! @deprecated(reason: "Useless field") anotherField: [Int] @something( lst: ["about" "stuff"] obj: {some: [4, 8, 16], complex: {about: 19.4}, another: EnumVal} ) fieldWithDefaultValueArg(test: String = "default"): ID simpleField: Date } """ generated_schema = build_graphql_schema_from_sdl(schema_sdl, schema=GraphQLSchema()) with pytest.raises(ValueError): generated_schema.get_field_by_name('Invalid.Field.name') with pytest.raises(ValueError): generated_schema.get_field_by_name('') with pytest.raises(ValueError): generated_schema.get_field_by_name('unknownField') # Happy path assert generated_schema.get_field_by_name( 'Query.nonDefaultField') is not None assert generated_schema.get_field_by_name( 'RootQuery.defaultField') is not None assert generated_schema.get_field_by_name('Test.field') is not None assert generated_schema.get_field_by_name('Test.simpleField') is not None # Sad path assert generated_schema.get_field_by_name('Something.unknownField') is None
async def test_resolver_decorator(): schema_sdl = """ schema @enable_cache { query: RootQuery mutation: RootMutation subscription: RootSubscription } scalar Date union Group = Foo | Bar | Baz interface Something { oneField: [Int] anotherField: [String] aLastOne: [[Date!]]! } input UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! } type RootQuery { defaultField: Int } # Query has been replaced by RootQuery as entrypoint type Query { nonDefaultField: String } \"\"\" This is a docstring for the Test Object Type. \"\"\" type Test implements Unknown & Empty @enable_cache { \"\"\" This is a field description :D \"\"\" field(input: InputObject): String! @deprecated(reason: "Useless field") anotherField: [Int] @something( lst: ["about" "stuff"] obj: {some: [4, 8, 16], complex: {about: 19.4}, another: EnumVal} ) fieldWithDefaultValueArg(test: String = "default"): ID simpleField: Date } """ generated_schema = build_graphql_schema_from_sdl(schema_sdl, schema=GraphQLSchema()) mock_one = Mock() mock_two = Mock() @Resolver("Test.field", schema=generated_schema) async def func_field_resolver(*args, **kwargs): mock_one() return @Resolver("RootQuery.defaultField", schema=generated_schema) async def func_default_resolver(*args, **kwargs): mock_two() return with pytest.raises(NonAwaitableResolver): @Resolver("Test.simpleField", schema=generated_schema) def func_default_resolver(*args, **kwargs): pass assert generated_schema.get_field_by_name( 'Test.field').resolver is not None assert callable(generated_schema.get_field_by_name('Test.field').resolver) assert mock_one.called is False assert generated_schema.get_field_by_name( 'RootQuery.defaultField').resolver is not None assert callable( generated_schema.get_field_by_name('RootQuery.defaultField').resolver) assert mock_two.called is False