本文整理汇总了TypeScript中graphql-tools.addMockFunctionsToSchema函数的典型用法代码示例。如果您正苦于以下问题:TypeScript addMockFunctionsToSchema函数的具体用法?TypeScript addMockFunctionsToSchema怎么用?TypeScript addMockFunctionsToSchema使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了addMockFunctionsToSchema函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('trace construction', async () => {
const typeDefs = `
type User {
id: Int
name: String
posts(limit: Int): [Post]
}
type Post {
id: Int
title: String
views: Int
author: User
}
type Query {
aString: String
aBoolean: Boolean
anInt: Int
author(id: Int): User
topPosts(limit: Int): [Post]
}
`;
const query = `
query q {
author(id: 5) {
name
posts(limit: 2) {
id
}
}
aBoolean
}
`;
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });
enableGraphQLExtensions(schema);
const traces: Array<any> = [];
function addTrace(signature: string, operationName: string, trace: Trace) {
traces.push({ signature, operationName, trace });
}
const reportingExtension = new EngineReportingExtension({}, addTrace);
const stack = new GraphQLExtensionStack([reportingExtension]);
const requestDidEnd = stack.requestDidStart({
request: new Request('http://localhost:123/foo') as any,
queryString: query,
});
await graphql({
schema,
source: query,
contextValue: { _extensionStack: stack },
});
requestDidEnd();
// XXX actually write some tests
});
示例2: createSchemeWithAccounts
export function createSchemeWithAccounts(accountsServer) {
const accountsGraphQL = createJSAccountsGraphQL(accountsServer);
// TODO after accounts pr remove dummy object
const mockResolvers = {
Query: {},
Mutation: {},
...subscriptionResolver
};
const resolversWithAccounts = accountsGraphQL.extendWithResolvers(mockResolvers);
const executableSchema = makeExecutableSchema({
typeDefs: [schema, accountsGraphQL.schema],
resolvers: resolversWithAccounts,
logger: { log: (e) => console.log(e) },
});
addMockFunctionsToSchema({ schema: executableSchema, mocks, preserveResolvers: true});
return executableSchema;
}
示例3: it
it("resolves an Artist on a Consignment Submission", async () => {
const allMergedSchemas = await mergeSchemas()
// This test is that a submission gets the artist by stitching a MP
// Artist into the ConsignmentSubmission inside the schema
const query = gql`
{
submission(id: 123) {
artist_id
artist {
name
}
}
}
`
// Mock the resolvers for just a submission, and an artist.
// The part we are testing is the step that goes from a submission
// to the Artist
addMockFunctionsToSchema({
schema: allMergedSchemas,
mocks: {
Query: () => ({
submission: (_root, _params) => {
return { artist_id: "321" }
},
}),
},
})
const result = await graphql(allMergedSchemas, query, {
accessToken: null,
userID: null,
})
expect(result).toEqual({
data: { submission: { artist: { name: "Hello World" }, artist_id: "321" } },
})
})
示例4: compose
export function compose(): AppFrontendLibs {
const appModule = uiModules.get('app/siem');
const observableApi = new AppKibanaObservableApiAdapter({
basePath: chrome.getBasePath(),
xsrfToken: chrome.getXsrfToken(),
});
const framework = new AppKibanaFrameworkAdapter(appModule, uiRoutes, timezoneProvider);
const typeDefs = `
Query {}
`;
const mocks = {
Mutation: () => undefined,
Query: () => undefined,
};
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({
mocks,
schema,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cache = new InMemoryCache((window as any).__APOLLO_CLIENT__);
const apolloClient = new ApolloClient({
cache,
link: new SchemaLink({ schema }),
});
const libs: AppFrontendLibs = {
apolloClient,
framework,
observableApi,
};
return libs;
}
示例5: require
import { GraphQLSchema } from 'graphql';
import { addApolloLogging } from 'apollo-logger';
import { makeExecutableSchema, addMockFunctionsToSchema, addErrorLoggingToSchema } from 'graphql-tools';
import { resolvers, typeDefs } from '@sample-stack/graphql-schema';
import { GraphQLAnyObject } from './scalar';
const rootSchemaDef = require('./root-schema.graphqls');
// import rootSchemaDef from './root_schema.graphqls';
import { logger } from '@sample-stack/utils';
import { pubsub } from '../container';
const DefaultResolver = {
AnyObject: GraphQLAnyObject,
};
const schema: GraphQLSchema = makeExecutableSchema({
resolvers: resolvers(pubsub, logger),
typeDefs: [rootSchemaDef].concat(typeDefs) as Array<any>,
});
addErrorLoggingToSchema(schema, { log: (e) => logger.error(e) });
addMockFunctionsToSchema({
mocks: {},
preserveResolvers: true,
schema,
});
export { schema };
示例6: makeExecutableSchema
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools'
import { graphql, ExecutionResult } from 'graphql'
import * as fs from 'fs-extra'
const typeDefs = fs.readFileSync(__dirname + '/cluster.graphql', 'utf-8')
const schema = makeExecutableSchema({ typeDefs })
addMockFunctionsToSchema({
schema,
mocks: {
Migration: () => ({
revision: 5,
}),
},
})
export const MockGraphQLClient = () => {
return {
request(query, variables) {
return graphql(schema, query, {}, {}, variables) as any
},
}
}
export { ExecutionResult }
示例7: makeExecutableSchema
// other option: https://github.com/MadRabbit/graphql-mock
import { SchemaLink } from 'apollo-link-schema';
import { addMockFunctionsToSchema, makeExecutableSchema } from 'graphql-tools';
import { typeDefs } from '../../server/schemas';
const schema = makeExecutableSchema({ typeDefs });
const mocks = {
Query: () => ({
books: () => [{ title: 'Book1 ', author: 'Tomas' }, { title: 'Book 2', author: 'Valeria' }]
})
};
addMockFunctionsToSchema({ mocks, schema });
export const schemaLink = new SchemaLink({ schema });
示例8: makeExecutableSchema
import { ApolloLink, Observable } from "apollo-link";
import { graphql, graphqlSync, print, introspectionQuery, IntrospectionQuery } from "graphql";
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import gql from "graphql-tag";
import { SubscriptionCovertLink, EntityInfo } from "../lib/";
import fs from "fs";
import path from "path";
import { CommentAdded } from "./graphql/CommentAdded";
const schemaString = fs.readFileSync(path.resolve(__dirname, "./schema.graphql"), { encoding: "utf8" });
const schema = makeExecutableSchema({ typeDefs: schemaString });
addMockFunctionsToSchema({ schema });
const introspectionResult = graphqlSync(schema, introspectionQuery).data! as any as IntrospectionQuery;
function delay(ms: number) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, ms);
});
}
export const link = new ApolloLink(operation => {
return new Observable(observer => {
const { query, operationName, variables } = operation;
(async () => {