bjornbytes / graphql-lua Goto Github PK
View Code? Open in Web Editor NEWGraphQL implementation in Lua
License: MIT License
GraphQL implementation in Lua
License: MIT License
(adding this to keep track of bug discovered)
schema/execute modules need to fix how interfaces/unions are defined and executed.
Either check that the resolveType is defined or provide a default resolver and check that all underlying types implement isTypeOf function.
Project = types.object {
name = 'Project',
fields = function()
return {
id = types.id.nonNull,
name = types.string.nonNull,
client_id = types.id.nonNull,
client = { -- resolver works
kind = Client,
resolve = function(project)
return getClientById(project.client_id)
end
}
tasks = { -- this fails
kind = types.list(Tasks),
resolve = function(project, arguments)
return getTasksByProjectId(project.id)
end
}
}
end
}
it fails with validate.lua:61: attempt to index field 'fields' (a nil value)
the line number might be wrong since i made a few changes
Is this the right place to fix this (accept custom resolver) or is it somewhere else?
Hints/help welcome :)
Hello. I am trying to render json based on executed schema and it works almost perfect.
I noticed that even if i declare args/arguments for a field - it doesn't produce any effect on executed schema.
I have tested it also on yours test example of schema.
there is a query
-- prebuilt query
local ast = parse [[
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type { ...TypeRef }
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
]]
-- Get variables
local variables = {}
-- get operation name
local operationName = 'IntrospectionQuery'
-- Validate a parsed query against a schema
local isValid = validate(schema, ast)
-- Execution
local rootValue = {}
local result = execute(schema, ast, rootValue, variables, operationName)
this query is built by graphiql by default.
so, usually then i use cjson of openresty
local json = cjson.encode(result)
-- json = string.gsub(json, '"interfaces":{},', '"interfaces":[],')
-- json = string.gsub(json, '"args":{},', '"args":[],')
ngx.say(json)
but also i've tried to pritn that table in console using print_r method from this article
Could you advice me where i could be wrong or how i could fix that behavior?
Thank you.
Is this production ready or is it missing some important features?
Show me a sample integrating KONG API management server and you'll have a fan and possible business partner for life!
@bjornbytes Thank you for merging my previous PRs. Before I send my next one, I wanted to discuss it with you (copying my comment into this issue from gitter).
The essential problem is that whatever JSON serialization library you use has to be able to distinguish between empty Lists and Objects as intended by GraphQL. Because Lua uses the same type, tables, for this purpose, it is non-obvious which one to emit. In our internal fork, we have patched graphql-lua
to require resty.libcjson
, which lets you set the metatable to indicate which type it should be in the output. If this is an acceptable dependency for you, I'll send the PR right away. Otherwise, I'd love to hear your thoughts on the matter.
Can you please provide an example of how to define two types that reference each other?
For example
local Client = types.object {
name = 'Client',
fields = {
id = types.id.nonNull,
name = types.string.nonNull,
-- projects = ...
}
}
local Project = types.object {
name = 'Project',
fields = {
id = types.id.nonNull,
name = types.string.nonNull,
client_id = types.id.nonNull
--client = ...
--tasks = ...
}
}
local Task = types.object {
name = 'Task',
fields = {
id = types.id.nonNull,
name = types.string.nonNull,
project_id = types.id.nonNull
--project = ...
}
}
If you have time and it's not too time consuming, a complete example of a schema based on these types and resolvers for them (in memory arrays) would help a lot.
The goal is to be able to issue queries like these
{
client (id: 1) {
id
name
projects {
id
name
tasks {
id
name
}
}
}
project (id: 1) {
id
name
client {
id
name
}
tasks {
id
name
}
}
}
Thank you for contributing this lib to open source, once i get a deeper understanding of the code i hope contribute back
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.