<commit_before>/**
 *  Copyright (c) 2015, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { extendSchema } from '../extendSchema';
import { execute } from '../../execution';
import { parse } from '../../language';
import { printSchema } from '../schemaPrinter';
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInterfaceType,
  GraphQLUnionType,
  GraphQLID,
  GraphQLString,
  GraphQLNonNull,
  GraphQLList,
} from '../../type';


// Test schema.
const SomeInterfaceType = new GraphQLInterfaceType({
  name: 'SomeInterface',
  resolveType: () => FooType,
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
  })
});

const FooType = new GraphQLObjectType({
  name: 'Foo',
  interfaces: [ SomeInterfaceType ],
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
    tree: { type: new GraphQLNonNull(new GraphQLList(FooType)) },
  })
});

/* eslint-disable no-unused-vars */
const BarType = new GraphQLObjectType({
  name: 'Bar',
  interfaces: [ SomeInterfaceType ],
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
    foo: { type: FooType },
  })
});
/* eslint-enable no-unused-vars */

const BizType = new GraphQLObjectType({
  name: 'Biz',
  fields: () => ({
    fizz: { type: GraphQLString },
  })
});

const SomeUnionType = new GraphQLUnionType({
  name: 'SomeUnion',
  resolveType: () => FooType,
  types: [ FooType, BizType ],
});

const testSchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      foo: { type: FooType },
      someUnion: { type: SomeUnionType },
      someInterface: {
        args: { id: { type: new GraphQLNonNull(GraphQLID) } },
        type: SomeInterfaceType
      },
    })
  })
});

describe('extendSchema', () => {

  it('returns the original schema when there are no type definitions', () => {
    const ast = parse('{ field }');
    const extendedSchema = extendSchema(testSchema, ast);
    expect(extendedSchema).to.equal(testSchema);
  });

  it('extends without altering original schema', () => {
    const ast = parse(`
      extend type Query {
        newField: String
      }
    `);
    const originalPrint = printSchema(testSchema);
    const extendedSchema = extendSchema(testSchema, ast);
    expect(extendSchema).to.not.equal(testSchema);
    expect(printSchema(testSchema)).to.equal(originalPrint);
    expect(printSchema(extendedSchema)).to.contain('newField');
    expect(printSchema(testSchema)).to.not.contain('newField');
  });

  it('cannot be used for execution', async () => {
    const ast = parse(`
      extend type Query {
        newField: String
      }
    `);
    const extendedSchema = extendSchema(testSchema, ast);
    const clientQuery = parse('{ newField }');

    const result = await execute(extendedSchema, clientQuery);
    expect(result.data.newField).to.equal(null);
    expect(result.errors).to.containSubset([ {
      message: 'Client Schema cannot be used for execution.'
    } ]);
  });

  it('extends objects by adding new fields', () => {
    const ast = parse(`<commit_msg>Test schema instead of function<commit_after>/**
 *  Copyright (c) 2015, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 */

import { describe, it } from 'mocha';
import { expect } from 'chai';
import { extendSchema } from '../extendSchema';
import { execute } from '../../execution';
import { parse } from '../../language';
import { printSchema } from '../schemaPrinter';
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInterfaceType,
  GraphQLUnionType,
  GraphQLID,
  GraphQLString,
  GraphQLNonNull,
  GraphQLList,
} from '../../type';


// Test schema.
const SomeInterfaceType = new GraphQLInterfaceType({
  name: 'SomeInterface',
  resolveType: () => FooType,
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
  })
});

const FooType = new GraphQLObjectType({
  name: 'Foo',
  interfaces: [ SomeInterfaceType ],
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
    tree: { type: new GraphQLNonNull(new GraphQLList(FooType)) },
  })
});

/* eslint-disable no-unused-vars */
const BarType = new GraphQLObjectType({
  name: 'Bar',
  interfaces: [ SomeInterfaceType ],
  fields: () => ({
    name: { type: GraphQLString },
    some: { type: SomeInterfaceType },
    foo: { type: FooType },
  })
});
/* eslint-enable no-unused-vars */

const BizType = new GraphQLObjectType({
  name: 'Biz',
  fields: () => ({
    fizz: { type: GraphQLString },
  })
});

const SomeUnionType = new GraphQLUnionType({
  name: 'SomeUnion',
  resolveType: () => FooType,
  types: [ FooType, BizType ],
});

const testSchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      foo: { type: FooType },
      someUnion: { type: SomeUnionType },
      someInterface: {
        args: { id: { type: new GraphQLNonNull(GraphQLID) } },
        type: SomeInterfaceType
      },
    })
  })
});

describe('extendSchema', () => {

  it('returns the original schema when there are no type definitions', () => {
    const ast = parse('{ field }');
    const extendedSchema = extendSchema(testSchema, ast);
    expect(extendedSchema).to.equal(testSchema);
  });

  it('extends without altering original schema', () => {
    const ast = parse(`
      extend type Query {
        newField: String
      }
    `);
    const originalPrint = printSchema(testSchema);
    const extendedSchema = extendSchema(testSchema, ast);
    expect(extendedSchema).to.not.equal(testSchema);
    expect(printSchema(testSchema)).to.equal(originalPrint);
    expect(printSchema(extendedSchema)).to.contain('newField');
    expect(printSchema(testSchema)).to.not.contain('newField');
  });

  it('cannot be used for execution', async () => {
    const ast = parse(`
      extend type Query {
        newField: String
      }
    `);
    const extendedSchema = extendSchema(testSchema, ast);
    const clientQuery = parse('{ newField }');

    const result = await execute(extendedSchema, clientQuery);
    expect(result.data.newField).to.equal(null);
    expect(result.errors).to.containSubset([ {
      message: 'Client Schema cannot be used for execution.'
    } ]);
  });

  it('extends objects by adding new fields', () => {
    const ast = parse(`
{'fraction_non_alphanumeric': 0.145748987854251, 'fraction_numerical': 0.001156737998843262, 'mean_word_length': 3.3806206459784676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479359', 'n_tokens_mistral': 2102, 'n_tokens_neox': 2064, 'n_words': 561}