我需要从graphql向python API发送一个Authorization请求,以便我可以与Python API进行通信。在此请求中,我需要使用Authorization
标头发送用户的用户名和密码,并接收将用于进一步通信的令牌。我不知道如何发送此请求。我创建了模式以进行通信,但无法弄清楚如何添加此部分。
以下是我的其余代码:
Schema.js
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
import fetch from 'node-fetch';
const BASE_URL = 'http://localhost:9000/api';
function getUserByURL(relativeURL) {
return fetch(`${BASE_URL}${relativeURL}`)
.then(res => res.json())
.then(json => json.user)
}
const UserType = new GraphQLObjectType({
name: 'User',
description: '...',
fields: () => ({
firstName: {
type: GraphQLString,
resolve: (user) => user.first_name
},
lastName: {
type: GraphQLString,
resolve: (user) => user.last_name
},
email: {type: GraphQLString},
username: {type: GraphQLString},
blockchain_address: {type: GraphQLString},
}),
});
const QueryType = new GraphQLObjectType({
name: 'Query',
description: '...',
fields: () => ({
user: {
type: UserType,
args: {
id: {type: GraphQLString,}
},
resolve: (root, args) =>
getUserByURL(`/users/${args.id}/`),
}
})
});
export default new GraphQLSchema({
query: QueryType,
});
index.js
import express from 'express';
import graphQLHTTP from 'express-graphql';
import schema from './schema';
const app = express();
app.use(graphQLHTTP({
schema,
graphiql: true,
}));
app.listen(8080);