我正在尝试在我的 AWS lambda 中运行 Apollo GraphQL 服务器。我正在使用这里的图书馆。我还使用 CDK 来部署 lambda 和 REST API 网关。
我的基础设施如下:
const helloFunction = new NodejsFunction(this, 'lambda', {
entry: path.join(__dirname, "lambda.ts"),
handler: "handler"
});
new LambdaRestApi(this, 'apigw', {
handler: helloFunction,
});
Run Code Online (Sandbox Code Playgroud)
lambda 实现如下:
const typeDefs = `#graphql
type Query {
hello: String
}`;
const resolvers = {
Query: {
hello: () => 'world',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
})
console.log('###? running lambda')
export const handler = startServerAndCreateLambdaHandler(
server, …Run Code Online (Sandbox Code Playgroud) 如何为graphql编写单元测试.我正在使用apollo服务器,graphql-tester和graphql.
当我运行测试时,它会出现以下错误
{ raw: '{"errors":[{"message":"Cannot read property \'definitions\' of undefined"}]}',
data: undefined,
errors: [ { message: 'Cannot read property \'definitions\' of undefined' } ],
headers:
{ 'x-powered-by': 'Express',
'content-type': 'application/json',
date: 'Wed, 18 Jan 2017 05:56:22 GMT',
connection: 'close',
'transfer-encoding': 'chunked' },
status: 400,
success: false }
1) Returns success
0 passing (35ms)
1 failing
1) Unittest1 Returns success:
TypeError: Cannot read property 'success' of undefined
at Assertion. (node_modules/chai/lib/chai/core/assertions.js:890:14)
at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
at Assertion.somethingMethod (node_modules/chai-things/lib/chai-things.js:97:25)
at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33) … I'm using apollo-server and testing using GraphiQL in my browser. I set up my resolvers based on Apollo's GitHunt-API example, but the resolver on the field "review.extraStuff" never gets called.
const rootResolvers = {
review(root, args, context) {
console.log('resolving review');
return {'HasErrors': true}
}
}
const extraStuff = (root, args, context) => {
console.log('resolving extraStuff');
return "yes";
}
rootResolvers.review.extraStuff = extraStuff;
export default {
RootQuery: rootResolvers
};
Run Code Online (Sandbox Code Playgroud)
const Review = `
type Review {
HasErrors: Boolean
extraStuff: …Run Code Online (Sandbox Code Playgroud) 我在graphql-tools文档中的任何地方都找不到应该如何利用enum所提供的模式中的类型makeExecutableSchema.任何人都知道这是怎么做到的?
示例代码:
enum Color {
RED
GREEN
BLUE
}
type Car {
color: Color!
}
Run Code Online (Sandbox Code Playgroud)
旋转变压器Color看起来像什么?
示例:https://codesandbox.io/s/j4mo8qpmrw
文档:https://www.apollographql.com/docs/link/links/state.html#default
TLDR:这是一个待办事项列表,@ client查询参数不会过滤掉列表.
这是查询,将$ id作为参数
const GET_TODOS = gql`
query todos($id: Int!) {
todos(id: $id) @client {
id
text
}
}
`;
Run Code Online (Sandbox Code Playgroud)
查询在那里传递变量
<Query query={GET_TODOS} variables={{ id: 1 }}>
/* Code */
</Query>
Run Code Online (Sandbox Code Playgroud)
但默认解析器不使用该参数,您可以在上面的codesandbox.io示例中看到它.
文档说它应该有用,但我似乎无法弄清楚我错过了什么.提前致谢!
如何复制:
server.js
const { ApolloServer, makeExecutableSchema, gql } = require('apollo-server');
const typeDefs = gql`
type Mutation {
uploadAvatar(upload: Upload!): String!
}
`;
const resolvers = {
Mutation: {
uploadAvatar(root, args, context, info) {
return 'test';
}
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({
schema,
});
server.listen().then(({ url }) => {
console.log(` Server ready at ${url}`);
});
Run Code Online (Sandbox Code Playgroud)
package.json
"dependencies": {
"apollo-server": "^2.0.0-rc.6",
"graphql": "^0.13.2"
}
Run Code Online (Sandbox Code Playgroud)
在节点server.js上,我们收到以下错误:
键入在文档中找不到的“上传”。
给定最新版本的apollo服务器,我是否应该在查询中添加其他内容?根据本教程和我目前不记得的其他一些资料,除了编写Upload之外,不需要做任何其他事情,它应该可以正常工作。我有什么想念的吗?
您可以使用GraphQL架构语言将数字用作GraphQL架构中的键吗?即(这是一个小片段...)
type tax_code_allocation_country_KOR_states {
"11": tax_code_allocation_state_tax_query
"26": tax_code_allocation_state_tax_query
"27": tax_code_allocation_state_tax_query
}
Run Code Online (Sandbox Code Playgroud)
或以下内容,我意识到这是错误的JSON:
type tax_code_allocation_country_KOR_states {
11: tax_code_allocation_state_tax_query
26: tax_code_allocation_state_tax_query
27: tax_code_allocation_state_tax_query
}
Run Code Online (Sandbox Code Playgroud) 在更新我们的GraphQL API时,仅_id需要model 字段,因此!在下面的SDL语言代码中。其他字段(例如,name不必包含在更新中)也不能具有null值。当前,!从名称字段中排除namein 允许最终用户不必在更新中传递in,但允许最终用户传递in的null值name,这是不允许的。
一个null值使我们知道需要从数据库中删除一个字段。
下面是一个可能导致问题的模型示例- Name自定义标量不允许空值,但GraphQL仍允许它们通过:
type language {
_id: ObjectId
iso: Language_ISO
auto_translate: Boolean
name: Name
updated_at: Date_time
created_at: Date_time
}
input language_create {
iso: Language_ISO!
auto_translate: Boolean
name: Name!
}
input language_update {
_id: ObjectId!
iso: Language_ISO!
auto_translate: Boolean
name: Name
}
Run Code Online (Sandbox Code Playgroud)
当传入null值时,它会绕过我们的Scalars,因此如果null不是允许的值,我们就不会引发用户输入验证错误。
我知道这!意味着non-nullable,并且缺少!手段意味着该字段可为空,但是令人沮丧的是,据我所知,如果某个字段不是必需的/可选的,我们无法为该字段指定确切的值。仅在更新时会发生此问题。
有什么方法可以通过自定义Scalars解决此问题,而不必在每个更新解析器中启动硬编码逻辑,而这似乎很麻烦?
可能失败的示例变异
mutation tests_language_create( $input: language_update! ) { …Run Code Online (Sandbox Code Playgroud) 我试图从一个突变中返回一个查询类型,在某些情况下我可以使它工作,但不能按我想要的方式工作。该问题与所使用的查询类型没有特别关系,因为我发现使用以外的其他类型具有相同的行为Query。
您可以在https://codesandbox.io/s/1z8kjy8m93上运行和修改此代码
服务器
const { ApolloServer, gql } = require("apollo-server");
const typeDefs = gql`
type Query {
hello(msg: String): String
}
type Mutation {
someMutation(someArg: String): MutationResponse
}
type MutationResponse {
query: Query
status: String
}
`;
const resolvers = {
Query: {
hello: (root, args, context) => {
console.log("hello: args = ", args);
return `${args.msg}, world !`;
}
},
Mutation: {
someMutation: (root, args, context) => {
console.log("someMutation: args = ", args);
return { status: …Run Code Online (Sandbox Code Playgroud) 我是GraphQL和Apollo Server的新手,尽管我已经搜索了文档和Google以获得答案。我正在使用apollo-server-express来从第三方REST API获取数据。REST API的字段使用snake_case。有没有简单的方法或Apollo Server规范方法将所有已解析的字段名称转换为camelCase?
我想使用驼峰式的情况来定义我的类型:
type SomeType {
id: ID!
createdTime: String
updatedTime: String
}
Run Code Online (Sandbox Code Playgroud)
但是REST API返回的对象如下:
{
"id": "1234"
"created_time": "2018-12-14T17:57:39+00:00",
"updated_time": "2018-12-14T17:57:39+00:00",
}
Run Code Online (Sandbox Code Playgroud)
我真的很想避免在我的解析器中手动规范化字段名称,即
Query: {
getObjects: () => new Promise((resolve, reject) => {
apiClient.get('/path/to/resource', (err, response) => {
if (err) {
return reject(err)
}
resolve(normalizeFields(response.entities))
})
})
}
Run Code Online (Sandbox Code Playgroud)
鉴于我希望解析器的数量很大,因此这种方法似乎容易出错。还感觉规范化字段名称不应该由解析程序负责。Apollo Server是否有某些功能可以让我批发标准化字段名称或覆盖默认字段分辨率?
apollo-server ×10
graphql ×8
apollo ×3
express ×2
graphql-js ×2
apollostack ×1
aws-lambda ×1
node.js ×1
react-apollo ×1