当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript graphql-extensions.enableGraphQLExtensions函数代码示例

本文整理汇总了TypeScript中graphql-extensions.enableGraphQLExtensions函数的典型用法代码示例。如果您正苦于以下问题:TypeScript enableGraphQLExtensions函数的具体用法?TypeScript enableGraphQLExtensions怎么用?TypeScript enableGraphQLExtensions使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了enableGraphQLExtensions函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
});
开发者ID:simonjoom,项目名称:react-native-project,代码行数:58,代码来源:extension.test.ts

示例2: collectCacheControlHints

export async function collectCacheControlHints(schema: GraphQLSchema, source: string, options?: CacheControlExtensionOptions): Promise<CacheHint[]> {
  enableGraphQLExtensions(schema);

  const cacheControlExtension = new CacheControlExtension(options);

  const response = await graphql({
    schema,
    source,
    contextValue: {
      _extensionStack: new GraphQLExtensionStack([cacheControlExtension])
    }
  });

  expect(response.errors).toBeUndefined();

  return cacheControlExtension.format()[1].hints;
}
开发者ID:cj5,项目名称:fantasy-golf-server,代码行数:17,代码来源:helpers.ts

示例3: initializeExtensionStack

  function initializeExtensionStack(): GraphQLExtensionStack<TContext> {
    enableGraphQLExtensions(config.schema);

    // If custom extension factories were provided, create per-request extension
    // objects.
    const extensions = config.extensions ? config.extensions.map(f => f()) : [];

    if (config.tracing) {
      extensions.push(new TracingExtension());
    }

    if (config.cacheControl) {
      cacheControlExtension = new CacheControlExtension(config.cacheControl);
      extensions.push(cacheControlExtension);
    }

    return new GraphQLExtensionStack(extensions);
  }
开发者ID:apollostack,项目名称:apollo-server,代码行数:18,代码来源:requestPipeline.ts

示例4: doRunQuery

function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
  let documentAST: DocumentNode;

  const logFunction =
    options.logFunction ||
    function() {
      return null;
    };
  const debugDefault =
    process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test';
  const debug = options.debug !== undefined ? options.debug : debugDefault;

  logFunction({ action: LogAction.request, step: LogStep.start });

  const context = options.context || {};
  let extensions = [];
  if (options.tracing) {
    extensions.push(TracingExtension);
  }
  if (options.cacheControl === true) {
    extensions.push(CacheControlExtension);
  } else if (options.cacheControl) {
    extensions.push(new CacheControlExtension(options.cacheControl));
  }
  const extensionStack =
    extensions.length > 0 && new GraphQLExtensionStack(extensions);

  if (extensionStack) {
    context._extensionStack = extensionStack;
    enableGraphQLExtensions(options.schema);

    extensionStack.requestDidStart();
  }

  const qry =
    typeof options.query === 'string' ? options.query : print(options.query);
  logFunction({
    action: LogAction.request,
    step: LogStep.status,
    key: 'query',
    data: qry,
  });
  logFunction({
    action: LogAction.request,
    step: LogStep.status,
    key: 'variables',
    data: options.variables,
  });
  logFunction({
    action: LogAction.request,
    step: LogStep.status,
    key: 'operationName',
    data: options.operationName,
  });

  // if query is already an AST, don't parse or validate
  // XXX: This refers the operations-store flow.
  if (typeof options.query === 'string') {
    try {
      logFunction({ action: LogAction.parse, step: LogStep.start });
      documentAST = parse(options.query as string);
      logFunction({ action: LogAction.parse, step: LogStep.end });
    } catch (syntaxError) {
      logFunction({ action: LogAction.parse, step: LogStep.end });
      return Promise.resolve({
        errors: format([syntaxError], options.formatError),
      });
    }
  } else {
    documentAST = options.query as DocumentNode;
  }

  let rules = specifiedRules;
  if (options.validationRules) {
    rules = rules.concat(options.validationRules);
  }
  logFunction({ action: LogAction.validation, step: LogStep.start });
  const validationErrors = validate(options.schema, documentAST, rules);
  logFunction({ action: LogAction.validation, step: LogStep.end });
  if (validationErrors.length) {
    return Promise.resolve({
      errors: format(validationErrors, options.formatError),
    });
  }

  if (extensionStack) {
    extensionStack.executionDidStart();
  }

  try {
    logFunction({ action: LogAction.execute, step: LogStep.start });
    return Promise.resolve(
      execute(
        options.schema,
        documentAST,
        options.rootValue,
        context,
        options.variables,
        options.operationName,
        options.fieldResolver,
//.........这里部分代码省略.........
开发者ID:chentsulin,项目名称:apollo-server,代码行数:101,代码来源:runQuery.ts

示例5: doRunQuery

function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
  if (options.queryString && options.parsedQuery) {
    throw new Error('Only supply one of queryString and parsedQuery');
  }
  if (!(options.queryString || options.parsedQuery)) {
    throw new Error('Must supply one of queryString and parsedQuery');
  }

  const debugDefault =
    process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test';
  const debug = options.debug !== undefined ? options.debug : debugDefault;

  const context = options.context || {};

  // If custom extension factories were provided, create per-request extension
  // objects.
  const extensions = options.extensions ? options.extensions.map(f => f()) : [];

  // If you're running behind an engineproxy, set these options to turn on
  // tracing and cache-control extensions.
  if (options.tracing) {
    extensions.push(new TracingExtension());
  }
  if (options.cacheControl === true) {
    extensions.push(new CacheControlExtension());
  } else if (options.cacheControl) {
    extensions.push(new CacheControlExtension(options.cacheControl));
  }

  const extensionStack = new GraphQLExtensionStack(extensions);

  // We unconditionally create an extensionStack, even if there are no
  // extensions (so that we don't have to litter the rest of this function with
  // `if (extensionStack)`, but we don't instrument the schema unless there
  // actually are extensions.  We do unconditionally put the stack on the
  // context, because if some other call had extensions and the schema is
  // already instrumented, that's the only way to get a custom fieldResolver to
  // work.
  if (extensions.length > 0) {
    enableGraphQLExtensions(options.schema);
  }
  context._extensionStack = extensionStack;

  const requestDidEnd = extensionStack.requestDidStart({
    // Since the Request interfacess are not the same between node-fetch and
    // typescript's lib dom, we should limit the fields that need to be passed
    // into requestDidStart to only the ones we need, currently just the
    // headers, method, and url
    request: options.request as any,
    queryString: options.queryString,
    parsedQuery: options.parsedQuery,
    operationName: options.operationName,
    variables: options.variables,
    persistedQueryHit: options.persistedQueryHit,
    persistedQueryRegister: options.persistedQueryRegister,
  });
  return Promise.resolve()
    .then(
      (): Promise<GraphQLResponse> => {
        // Parse the document.
        let documentAST: DocumentNode;
        if (options.parsedQuery) {
          documentAST = options.parsedQuery;
        } else if (!options.queryString) {
          throw new Error('Must supply one of queryString and parsedQuery');
        } else {
          const parsingDidEnd = extensionStack.parsingDidStart({
            queryString: options.queryString,
          });
          let graphqlParseErrors: SyntaxError[] | undefined;
          try {
            documentAST = parse(options.queryString);
          } catch (syntaxError) {
            graphqlParseErrors = formatApolloErrors(
              [
                fromGraphQLError(syntaxError, {
                  errorClass: SyntaxError,
                }),
              ],
              {
                debug,
              },
            );
            return Promise.resolve({ errors: graphqlParseErrors });
          } finally {
            parsingDidEnd(...(graphqlParseErrors || []));
          }
        }

        if (
          options.nonQueryError &&
          !isQueryOperation(documentAST, options.operationName)
        ) {
          // XXX this goes to requestDidEnd, is that correct or should it be
          // validation?
          throw options.nonQueryError;
        }

        let rules = specifiedRules;
        if (options.validationRules) {
//.........这里部分代码省略.........
开发者ID:simonjoom,项目名称:react-native-project,代码行数:101,代码来源:runQuery.ts


注:本文中的graphql-extensions.enableGraphQLExtensions函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。