const GraphQL = require('graphql');
const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    graphql
} = GraphQL;

const graphqlMiddleware = require('graphql-middleware').default;

const ExampleType = new GraphQLObjectType({
    name: 'Example',
    fields: {
        id: {
            type: GraphQLString
        },
        name: {
            type: GraphQLString
        }
    }
})

const querySchema = graphqlMiddleware({
    name: 'RootQueryType',
    fields: {
        hello: {
            beforeResolve (root, args, ctx) {
                throw new Error('Test');
            },
            type: GraphQLString,
            resolve (root, args, ctx) {
                return 'world';
            }
        },
        haha: {
            type: ExampleType,
            
 () {
                return {
                    id: 1
                };
            }
        }
    }
}, [
    (root, args, ctx) => {
        // throw new Error('Applies to all');
    },
    (root, args, ctx) => {
        throw new Error('Applies to all after first');
    },
]);

const schema = new GraphQLSchema({
    query: new GraphQLObjectType(querySchema)
});

const query = '{ hello }';

graphql(schema, query).then((res) => {
    console.log(res);
}).catch((err) => {
    console.error(err.stack);
});

const queryTwo = '{ haha { id } }';

graphql(schema, queryTwo).then((res) => {
    console.log(res);
}).catch((err) => {
    console.error(err.stack);
});

resolve