|
| 1 | +/** |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +import { expect } from 'chai'; |
| 11 | +import { describe, it } from 'mocha'; |
| 12 | + |
| 13 | +import { |
| 14 | + graphql, |
| 15 | + GraphQLSchema, |
| 16 | + GraphQLObjectType, |
| 17 | + GraphQLString, |
| 18 | + GraphQLInt, |
| 19 | +} from '../../'; |
| 20 | + |
| 21 | +describe('Execute: resolve function', () => { |
| 22 | + |
| 23 | + function testSchema(testField) { |
| 24 | + return new GraphQLSchema({ |
| 25 | + query: new GraphQLObjectType({ |
| 26 | + name: 'Query', |
| 27 | + fields: { |
| 28 | + test: testField |
| 29 | + } |
| 30 | + }) |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + it('default function accesses properties', async () => { |
| 35 | + const schema = testSchema({ type: GraphQLString }); |
| 36 | + |
| 37 | + const source = { |
| 38 | + test: 'testValue' |
| 39 | + }; |
| 40 | + |
| 41 | + expect( |
| 42 | + await graphql(schema, '{ test }', source) |
| 43 | + ).to.deep.equal({ |
| 44 | + data: { |
| 45 | + test: 'testValue' |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('default function calls methods', async () => { |
| 51 | + const schema = testSchema({ type: GraphQLString }); |
| 52 | + |
| 53 | + const source = { |
| 54 | + _secret: 'secretValue', |
| 55 | + test() { |
| 56 | + return this._secret; |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + expect( |
| 61 | + await graphql(schema, '{ test }', source) |
| 62 | + ).to.deep.equal({ |
| 63 | + data: { |
| 64 | + test: 'secretValue' |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('uses provided resolve function', async () => { |
| 70 | + const schema = testSchema({ |
| 71 | + type: GraphQLString, |
| 72 | + args: { |
| 73 | + aStr: { type: GraphQLString }, |
| 74 | + aInt: { type: GraphQLInt }, |
| 75 | + }, |
| 76 | + resolve(source, args) { |
| 77 | + return JSON.stringify([ source, args ]); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + expect( |
| 82 | + await graphql(schema, '{ test }') |
| 83 | + ).to.deep.equal({ |
| 84 | + data: { |
| 85 | + test: '[null,{}]' |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + expect( |
| 90 | + await graphql(schema, '{ test }', 'Source!') |
| 91 | + ).to.deep.equal({ |
| 92 | + data: { |
| 93 | + test: '["Source!",{}]' |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + expect( |
| 98 | + await graphql(schema, '{ test(aStr: "String!") }', 'Source!') |
| 99 | + ).to.deep.equal({ |
| 100 | + data: { |
| 101 | + test: '["Source!",{"aStr":"String!"}]' |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + expect( |
| 106 | + await graphql(schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!') |
| 107 | + ).to.deep.equal({ |
| 108 | + data: { |
| 109 | + test: '["Source!",{"aStr":"String!","aInt":-123}]' |
| 110 | + } |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | +}); |
0 commit comments