def test_cannot_use_client_schema_for_general_execution(): customScalar = GraphQLScalarType(name='CustomScalar', serialize=lambda: None) schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields={ 'foo': GraphQLField(GraphQLString, args=OrderedDict([('custom1', GraphQLArgument(customScalar)), ('custom2', GraphQLArgument(customScalar))])) })) introspection = graphql(schema, introspection_query) client_schema = build_client_schema(introspection.data) class data: foo = 'bar' result = graphql( client_schema, 'query NoNo($v: CustomScalar) { foo(custom1: 123, custom2: $v) }', data, {'v': 'baz'}) assert result.data == {'foo': None} assert [format_error(e) for e in result.errors] == [{ 'locations': [{ 'column': 32, 'line': 1 }], 'message': 'Client Schema cannot be used for execution.' }]
def test_uses_built_in_scalars_when_possible(): customScalar = GraphQLScalarType( name='CustomScalar', serialize=lambda: None ) schema = GraphQLSchema( query=GraphQLObjectType( name='Scalars', fields=OrderedDict([ ('int', GraphQLField(GraphQLInt)), ('float', GraphQLField(GraphQLFloat)), ('string', GraphQLField(GraphQLString)), ('boolean', GraphQLField(GraphQLBoolean)), ('id', GraphQLField(GraphQLID)), ('custom', GraphQLField(customScalar)), ]) ) ) client_schema = _test_schema(schema) assert client_schema.get_type('Int') == GraphQLInt assert client_schema.get_type('Float') == GraphQLFloat assert client_schema.get_type('String') == GraphQLString assert client_schema.get_type('Boolean') == GraphQLBoolean assert client_schema.get_type('ID') == GraphQLID assert client_schema.get_type('CustomScalar') != customScalar
def internal_type(cls, schema): serialize = getattr(cls, 'serialize') parse_literal = getattr(cls, 'parse_literal') parse_value = getattr(cls, 'parse_value') return GraphQLScalarType(name=cls._meta.type_name, description=cls._meta.description, serialize=serialize, parse_value=parse_value, parse_literal=parse_literal)
def test_prints_custom_scalar(): OddType = GraphQLScalarType(name='Odd', serialize=lambda v: v if v % 2 == 1 else None) Root = GraphQLObjectType(name='Root', fields={'odd': GraphQLField(OddType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLArgument, GraphQLInputObjectField, GraphQLInputObjectType, GraphQLList, GraphQLString, GraphQLNonNull, GraphQLScalarType, ) from graphql.core.error import GraphQLError, format_error TestComplexScalar = GraphQLScalarType( name='ComplexScalar', serialize=lambda v: 'SerializedValue' if v == 'DeserializedValue' else None, parse_value=lambda v: 'DeserializedValue' if v == 'SerializedValue' else None, parse_literal=lambda v: 'DeserializedValue' if v.value == 'SerializedValue' else None ) TestInputObject = GraphQLInputObjectType('TestInputObject', { 'a': GraphQLInputObjectField(GraphQLString), 'b': GraphQLInputObjectField(GraphQLList(GraphQLString)), 'c': GraphQLInputObjectField(GraphQLNonNull(GraphQLString)), 'd': GraphQLInputObjectField(TestComplexScalar) }) stringify = lambda obj: json.dumps(obj, sort_keys=True) def input_to_json(obj, args, info): input = args.get('input')