代码之家  ›  专栏  ›  技术社区  ›  Douglas Woods

Apollo服务器:如何将授权令牌传递给远程模式

  •  0
  • Douglas Woods  · 技术社区  · 5 年前

    import {makeRemoteExecutableSchema, introspectSchema, mergeSchemas} from 'graphql-tools';
    import {HttpLink} from 'apollo-link-http';
    import {ApolloServer} from 'apollo-server';
    import fetch from 'node-fetch';
    
    // create executable schemas from remote GraphQL APIs
    const customFetch = (uri, options) => {
      // How do I set the token from my server req??
      options.headers.Authorization =
        'Bearer eyJhbGciOiJIUzI1NiIsI Not A Real Token kf5iOg9SkxDBVtQnLJuz3hXEDA';
      return fetch(uri, options);
    };
    
    const createRemoteExecutableSchemas = async () => {
      let schemas = [];
      const link = new HttpLink({
        uri: 'http://remote.graphql.server:5555/graphql',
        fetch: customFetch,
      });
      const remoteSchema = await introspectSchema(link);
      const remoteExecutableSchema = makeRemoteExecutableSchema({
        schema: remoteSchema,
        link,
      });
      schemas.push(remoteExecutableSchema);
      return schemas;
    };
    
    const createNewSchema = async () => {
      const schemas = await createRemoteExecutableSchemas();
      return mergeSchemas({
        schemas,
      });
    };
    
    const runServer = async () => {
      const schema = await createNewSchema();
      const server = new ApolloServer({
        schema
      });
      server.listen().then(({url}) => {
        console.log(`🚀  Server ready at ${url}`);
      });
    };
    
    try {
      runServer();
    } catch (err) {
      console.error(err);
    }
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   Shai    5 年前

    我一个人为此奋斗了一段时间。 尝试下一种方法:

    const express = require('express');
    const { introspectSchema, makeRemoteExecutableSchema, ApolloServer } = require('apollo-server-express');
    const { HttpLink } = require('apollo-link-http');
    const fetch = require('node-fetch');
    const { setContext } = require('apollo-link-context');
    
    async function runServer() {
        const httpLink = new HttpLink({ uri: 'http://remote.graphql.server:5555/graphql', fetch });
        const link = setContext((request, prevContext) => {
            if (prevContext.graphqlContext && prevContext.graphqlContext.headers) {
                return {
                    headers: {
                        'Authorization': prevContext.graphqlContext.headers.authorization,
                    }
                }
            } else {
                return {}
            }
        }).concat(httpLink);
    
        const schema = await introspectSchema(link);
    
        const executableSchema = makeRemoteExecutableSchema({
            schema,
            link,
        });
    
        const server = new ApolloServer({
            schema: executableSchema,
            context: ({ req }) => ({
                headers: req.headers
              }),
        });
    
        // middleware
        const app = express();
        server.applyMiddleware({ app });
    
        app.listen({ port: 4000 }, () =>
            console.log(
                '🚀 Server ready at localhost:4000',
            )
        )
    }
    
    runServer();