feat: add support for graphql fragments
This commit is contained in:
parent
a775f9475b
commit
33f3fefbf3
|
@ -196,8 +196,6 @@ func (c *scontext) resolvePreparedSQL() ([]byte, *stmt, error) {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(">>", varsList)
|
|
||||||
|
|
||||||
if useTx {
|
if useTx {
|
||||||
row = tx.Stmt(q.sd).QueryRow(varsList...)
|
row = tx.Stmt(q.sd).QueryRow(varsList...)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -307,6 +307,80 @@ func multiRoot(t *testing.T) {
|
||||||
compileGQLToPSQL(t, gql, nil, "user")
|
compileGQLToPSQL(t, gql, nil, "user")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withFragment1(t *testing.T) {
|
||||||
|
gql := `
|
||||||
|
fragment userFields1 on user {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
|
created_at
|
||||||
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}`
|
||||||
|
|
||||||
|
compileGQLToPSQL(t, gql, nil, "anon")
|
||||||
|
}
|
||||||
|
|
||||||
|
func withFragment2(t *testing.T) {
|
||||||
|
gql := `
|
||||||
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
|
created_at
|
||||||
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields1 on user {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}`
|
||||||
|
|
||||||
|
compileGQLToPSQL(t, gql, nil, "anon")
|
||||||
|
}
|
||||||
|
|
||||||
|
func withFragment3(t *testing.T) {
|
||||||
|
gql := `
|
||||||
|
|
||||||
|
fragment userFields1 on user {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}
|
||||||
|
|
||||||
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
|
created_at
|
||||||
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
compileGQLToPSQL(t, gql, nil, "anon")
|
||||||
|
}
|
||||||
|
|
||||||
func withCursor(t *testing.T) {
|
func withCursor(t *testing.T) {
|
||||||
gql := `query {
|
gql := `query {
|
||||||
Products(
|
Products(
|
||||||
|
@ -400,6 +474,9 @@ func TestCompileQuery(t *testing.T) {
|
||||||
t.Run("queryWithVariables", queryWithVariables)
|
t.Run("queryWithVariables", queryWithVariables)
|
||||||
t.Run("withWhereOnRelations", withWhereOnRelations)
|
t.Run("withWhereOnRelations", withWhereOnRelations)
|
||||||
t.Run("multiRoot", multiRoot)
|
t.Run("multiRoot", multiRoot)
|
||||||
|
t.Run("withFragment1", withFragment1)
|
||||||
|
t.Run("withFragment2", withFragment2)
|
||||||
|
t.Run("withFragment3", withFragment3)
|
||||||
t.Run("jsonColumnAsTable", jsonColumnAsTable)
|
t.Run("jsonColumnAsTable", jsonColumnAsTable)
|
||||||
t.Run("withCursor", withCursor)
|
t.Run("withCursor", withCursor)
|
||||||
t.Run("nullForAuthRequiredInAnon", nullForAuthRequiredInAnon)
|
t.Run("nullForAuthRequiredInAnon", nullForAuthRequiredInAnon)
|
||||||
|
|
|
@ -86,6 +86,12 @@ SELECT jsonb_build_object('product', "__sj_0"."json") as "__root" FROM (SELECT t
|
||||||
SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "users_0"."id" AS "id", "users_0"."email" AS "email" FROM (SELECT "users"."id", "users"."email" FROM "users" WHERE (NOT EXISTS (SELECT 1 FROM products WHERE (("products"."user_id") = ("users"."id")) AND ((("products"."price") > '3' :: numeric(7,2))))) LIMIT ('20') :: integer) AS "users_0") AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "users_0"."id" AS "id", "users_0"."email" AS "email" FROM (SELECT "users"."id", "users"."email" FROM "users" WHERE (NOT EXISTS (SELECT 1 FROM products WHERE (("products"."user_id") = ("users"."id")) AND ((("products"."price") > '3' :: numeric(7,2))))) LIMIT ('20') :: integer) AS "users_0") AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
||||||
=== RUN TestCompileQuery/multiRoot
|
=== RUN TestCompileQuery/multiRoot
|
||||||
SELECT jsonb_build_object('customer', "__sj_0"."json", 'user', "__sj_1"."json", 'product', "__sj_2"."json") as "__root" FROM (SELECT to_jsonb("__sr_2".*) AS "json"FROM (SELECT "products_2"."id" AS "id", "products_2"."name" AS "name", "__sj_3"."json" AS "customers", "__sj_4"."json" AS "customer" FROM (SELECT "products"."id", "products"."name" FROM "products" WHERE (((("products"."price") > '0' :: numeric(7,2)) AND (("products"."price") < '8' :: numeric(7,2)))) LIMIT ('1') :: integer) AS "products_2" LEFT OUTER JOIN LATERAL (SELECT to_jsonb("__sr_4".*) AS "json"FROM (SELECT "customers_4"."email" AS "email" FROM (SELECT "customers"."email" FROM "customers" LEFT OUTER JOIN "purchases" ON (("purchases"."product_id") = ("products_2"."id")) WHERE ((("customers"."id") = ("purchases"."customer_id"))) LIMIT ('1') :: integer) AS "customers_4") AS "__sr_4") AS "__sj_4" ON ('true') LEFT OUTER JOIN LATERAL (SELECT coalesce(jsonb_agg("__sj_3"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_3".*) AS "json"FROM (SELECT "customers_3"."email" AS "email" FROM (SELECT "customers"."email" FROM "customers" LEFT OUTER JOIN "purchases" ON (("purchases"."product_id") = ("products_2"."id")) WHERE ((("customers"."id") = ("purchases"."customer_id"))) LIMIT ('20') :: integer) AS "customers_3") AS "__sr_3") AS "__sj_3") AS "__sj_3" ON ('true')) AS "__sr_2") AS "__sj_2", (SELECT to_jsonb("__sr_1".*) AS "json"FROM (SELECT "users_1"."id" AS "id", "users_1"."email" AS "email" FROM (SELECT "users"."id", "users"."email" FROM "users" LIMIT ('1') :: integer) AS "users_1") AS "__sr_1") AS "__sj_1", (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "customers_0"."id" AS "id" FROM (SELECT "customers"."id" FROM "customers" LIMIT ('1') :: integer) AS "customers_0") AS "__sr_0") AS "__sj_0"
|
SELECT jsonb_build_object('customer', "__sj_0"."json", 'user', "__sj_1"."json", 'product', "__sj_2"."json") as "__root" FROM (SELECT to_jsonb("__sr_2".*) AS "json"FROM (SELECT "products_2"."id" AS "id", "products_2"."name" AS "name", "__sj_3"."json" AS "customers", "__sj_4"."json" AS "customer" FROM (SELECT "products"."id", "products"."name" FROM "products" WHERE (((("products"."price") > '0' :: numeric(7,2)) AND (("products"."price") < '8' :: numeric(7,2)))) LIMIT ('1') :: integer) AS "products_2" LEFT OUTER JOIN LATERAL (SELECT to_jsonb("__sr_4".*) AS "json"FROM (SELECT "customers_4"."email" AS "email" FROM (SELECT "customers"."email" FROM "customers" LEFT OUTER JOIN "purchases" ON (("purchases"."product_id") = ("products_2"."id")) WHERE ((("customers"."id") = ("purchases"."customer_id"))) LIMIT ('1') :: integer) AS "customers_4") AS "__sr_4") AS "__sj_4" ON ('true') LEFT OUTER JOIN LATERAL (SELECT coalesce(jsonb_agg("__sj_3"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_3".*) AS "json"FROM (SELECT "customers_3"."email" AS "email" FROM (SELECT "customers"."email" FROM "customers" LEFT OUTER JOIN "purchases" ON (("purchases"."product_id") = ("products_2"."id")) WHERE ((("customers"."id") = ("purchases"."customer_id"))) LIMIT ('20') :: integer) AS "customers_3") AS "__sr_3") AS "__sj_3") AS "__sj_3" ON ('true')) AS "__sr_2") AS "__sj_2", (SELECT to_jsonb("__sr_1".*) AS "json"FROM (SELECT "users_1"."id" AS "id", "users_1"."email" AS "email" FROM (SELECT "users"."id", "users"."email" FROM "users" LIMIT ('1') :: integer) AS "users_1") AS "__sr_1") AS "__sj_1", (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "customers_0"."id" AS "id" FROM (SELECT "customers"."id" FROM "customers" LIMIT ('1') :: integer) AS "customers_0") AS "__sr_0") AS "__sj_0"
|
||||||
|
=== RUN TestCompileQuery/withFragment1
|
||||||
|
SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "users_0"."first_name" AS "first_name", "users_0"."last_name" AS "last_name", "users_0"."created_at" AS "created_at", "users_0"."id" AS "id", "users_0"."email" AS "email" FROM (SELECT , "users"."created_at", "users"."id", "users"."email" FROM "users" GROUP BY "users"."created_at", "users"."id", "users"."email" LIMIT ('20') :: integer) AS "users_0") AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
||||||
|
=== RUN TestCompileQuery/withFragment2
|
||||||
|
SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "users_0"."first_name" AS "first_name", "users_0"."last_name" AS "last_name", "users_0"."created_at" AS "created_at", "users_0"."id" AS "id", "users_0"."email" AS "email" FROM (SELECT , "users"."created_at", "users"."id", "users"."email" FROM "users" GROUP BY "users"."created_at", "users"."id", "users"."email" LIMIT ('20') :: integer) AS "users_0") AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
||||||
|
=== RUN TestCompileQuery/withFragment3
|
||||||
|
SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "users_0"."first_name" AS "first_name", "users_0"."last_name" AS "last_name", "users_0"."created_at" AS "created_at", "users_0"."id" AS "id", "users_0"."email" AS "email" FROM (SELECT , "users"."created_at", "users"."id", "users"."email" FROM "users" GROUP BY "users"."created_at", "users"."id", "users"."email" LIMIT ('20') :: integer) AS "users_0") AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
||||||
=== RUN TestCompileQuery/jsonColumnAsTable
|
=== RUN TestCompileQuery/jsonColumnAsTable
|
||||||
SELECT jsonb_build_object('products', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name", "__sj_1"."json" AS "tag_count" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('20') :: integer) AS "products_0" LEFT OUTER JOIN LATERAL (SELECT to_jsonb("__sr_1".*) AS "json"FROM (SELECT "tag_count_1"."count" AS "count", "__sj_2"."json" AS "tags" FROM (SELECT "tag_count"."count", "tag_count"."tag_id" FROM "products", json_to_recordset("products"."tag_count") AS "tag_count"(tag_id bigint, count int) WHERE ((("products"."id") = ("products_0"."id"))) LIMIT ('1') :: integer) AS "tag_count_1" LEFT OUTER JOIN LATERAL (SELECT coalesce(jsonb_agg("__sj_2"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_2".*) AS "json"FROM (SELECT "tags_2"."name" AS "name" FROM (SELECT "tags"."name" FROM "tags" WHERE ((("tags"."id") = ("tag_count_1"."tag_id"))) LIMIT ('20') :: integer) AS "tags_2") AS "__sr_2") AS "__sj_2") AS "__sj_2" ON ('true')) AS "__sr_1") AS "__sj_1" ON ('true')) AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
SELECT jsonb_build_object('products', "__sj_0"."json") as "__root" FROM (SELECT coalesce(jsonb_agg("__sj_0"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_0".*) AS "json"FROM (SELECT "products_0"."id" AS "id", "products_0"."name" AS "name", "__sj_1"."json" AS "tag_count" FROM (SELECT "products"."id", "products"."name" FROM "products" LIMIT ('20') :: integer) AS "products_0" LEFT OUTER JOIN LATERAL (SELECT to_jsonb("__sr_1".*) AS "json"FROM (SELECT "tag_count_1"."count" AS "count", "__sj_2"."json" AS "tags" FROM (SELECT "tag_count"."count", "tag_count"."tag_id" FROM "products", json_to_recordset("products"."tag_count") AS "tag_count"(tag_id bigint, count int) WHERE ((("products"."id") = ("products_0"."id"))) LIMIT ('1') :: integer) AS "tag_count_1" LEFT OUTER JOIN LATERAL (SELECT coalesce(jsonb_agg("__sj_2"."json"), '[]') as "json" FROM (SELECT to_jsonb("__sr_2".*) AS "json"FROM (SELECT "tags_2"."name" AS "name" FROM (SELECT "tags"."name" FROM "tags" WHERE ((("tags"."id") = ("tag_count_1"."tag_id"))) LIMIT ('20') :: integer) AS "tags_2") AS "__sr_2") AS "__sj_2") AS "__sj_2" ON ('true')) AS "__sr_1") AS "__sj_1" ON ('true')) AS "__sr_0") AS "__sj_0") AS "__sj_0"
|
||||||
=== RUN TestCompileQuery/withCursor
|
=== RUN TestCompileQuery/withCursor
|
||||||
|
@ -117,6 +123,9 @@ SELECT jsonb_build_object('users', "__sj_0"."json") as "__root" FROM (SELECT coa
|
||||||
--- PASS: TestCompileQuery/queryWithVariables (0.00s)
|
--- PASS: TestCompileQuery/queryWithVariables (0.00s)
|
||||||
--- PASS: TestCompileQuery/withWhereOnRelations (0.00s)
|
--- PASS: TestCompileQuery/withWhereOnRelations (0.00s)
|
||||||
--- PASS: TestCompileQuery/multiRoot (0.00s)
|
--- PASS: TestCompileQuery/multiRoot (0.00s)
|
||||||
|
--- PASS: TestCompileQuery/withFragment1 (0.00s)
|
||||||
|
--- PASS: TestCompileQuery/withFragment2 (0.00s)
|
||||||
|
--- PASS: TestCompileQuery/withFragment3 (0.00s)
|
||||||
--- PASS: TestCompileQuery/jsonColumnAsTable (0.00s)
|
--- PASS: TestCompileQuery/jsonColumnAsTable (0.00s)
|
||||||
--- PASS: TestCompileQuery/withCursor (0.00s)
|
--- PASS: TestCompileQuery/withCursor (0.00s)
|
||||||
--- PASS: TestCompileQuery/nullForAuthRequiredInAnon (0.00s)
|
--- PASS: TestCompileQuery/nullForAuthRequiredInAnon (0.00s)
|
||||||
|
@ -151,4 +160,4 @@ WITH "_sg_input" AS (SELECT $1 :: json AS j), "_x_users" AS (SELECT * FROM (VALU
|
||||||
--- PASS: TestCompileUpdate/nestedUpdateOneToOneWithConnect (0.00s)
|
--- PASS: TestCompileUpdate/nestedUpdateOneToOneWithConnect (0.00s)
|
||||||
--- PASS: TestCompileUpdate/nestedUpdateOneToOneWithDisconnect (0.00s)
|
--- PASS: TestCompileUpdate/nestedUpdateOneToOneWithDisconnect (0.00s)
|
||||||
PASS
|
PASS
|
||||||
ok github.com/dosco/super-graph/core/internal/psql (cached)
|
ok github.com/dosco/super-graph/core/internal/psql 0.374s
|
||||||
|
|
|
@ -11,15 +11,18 @@ import (
|
||||||
var (
|
var (
|
||||||
queryToken = []byte("query")
|
queryToken = []byte("query")
|
||||||
mutationToken = []byte("mutation")
|
mutationToken = []byte("mutation")
|
||||||
|
fragmentToken = []byte("fragment")
|
||||||
subscriptionToken = []byte("subscription")
|
subscriptionToken = []byte("subscription")
|
||||||
|
onToken = []byte("on")
|
||||||
trueToken = []byte("true")
|
trueToken = []byte("true")
|
||||||
falseToken = []byte("false")
|
falseToken = []byte("false")
|
||||||
quotesToken = []byte(`'"`)
|
quotesToken = []byte(`'"`)
|
||||||
signsToken = []byte(`+-`)
|
signsToken = []byte(`+-`)
|
||||||
punctuatorToken = []byte(`!():=[]{|}`)
|
|
||||||
spreadToken = []byte(`...`)
|
spreadToken = []byte(`...`)
|
||||||
digitToken = []byte(`0123456789`)
|
digitToken = []byte(`0123456789`)
|
||||||
dotToken = []byte(`.`)
|
dotToken = []byte(`.`)
|
||||||
|
|
||||||
|
punctuatorToken = `!():=[]{|}`
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pos represents a byte position in the original input text from which
|
// Pos represents a byte position in the original input text from which
|
||||||
|
@ -43,6 +46,8 @@ const (
|
||||||
itemName
|
itemName
|
||||||
itemQuery
|
itemQuery
|
||||||
itemMutation
|
itemMutation
|
||||||
|
itemFragment
|
||||||
|
itemOn
|
||||||
itemSub
|
itemSub
|
||||||
itemPunctuator
|
itemPunctuator
|
||||||
itemArgsOpen
|
itemArgsOpen
|
||||||
|
@ -263,12 +268,12 @@ func lexRoot(l *lexer) stateFn {
|
||||||
l.backup()
|
l.backup()
|
||||||
return lexString
|
return lexString
|
||||||
case r == '.':
|
case r == '.':
|
||||||
if len(l.input) >= 3 {
|
l.acceptRun(dotToken)
|
||||||
if equals(l.input, 0, 3, spreadToken) {
|
s, e := l.current()
|
||||||
|
if equals(l.input, s, e, spreadToken) {
|
||||||
l.emit(itemSpread)
|
l.emit(itemSpread)
|
||||||
return lexRoot
|
return lexRoot
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fallthrough // '.' can start a number.
|
fallthrough // '.' can start a number.
|
||||||
case r == '+' || r == '-' || ('0' <= r && r <= '9'):
|
case r == '+' || r == '-' || ('0' <= r && r <= '9'):
|
||||||
l.backup()
|
l.backup()
|
||||||
|
@ -299,10 +304,14 @@ func lexName(l *lexer) stateFn {
|
||||||
switch {
|
switch {
|
||||||
case equals(l.input, s, e, queryToken):
|
case equals(l.input, s, e, queryToken):
|
||||||
l.emitL(itemQuery)
|
l.emitL(itemQuery)
|
||||||
|
case equals(l.input, s, e, fragmentToken):
|
||||||
|
l.emitL(itemFragment)
|
||||||
case equals(l.input, s, e, mutationToken):
|
case equals(l.input, s, e, mutationToken):
|
||||||
l.emitL(itemMutation)
|
l.emitL(itemMutation)
|
||||||
case equals(l.input, s, e, subscriptionToken):
|
case equals(l.input, s, e, subscriptionToken):
|
||||||
l.emitL(itemSub)
|
l.emitL(itemSub)
|
||||||
|
case equals(l.input, s, e, onToken):
|
||||||
|
l.emitL(itemOn)
|
||||||
case equals(l.input, s, e, trueToken):
|
case equals(l.input, s, e, trueToken):
|
||||||
l.emitL(itemBoolVal)
|
l.emitL(itemBoolVal)
|
||||||
case equals(l.input, s, e, falseToken):
|
case equals(l.input, s, e, falseToken):
|
||||||
|
@ -396,31 +405,11 @@ func isAlphaNumeric(r rune) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func equals(b []byte, s Pos, e Pos, val []byte) bool {
|
func equals(b []byte, s Pos, e Pos, val []byte) bool {
|
||||||
n := 0
|
return bytes.EqualFold(b[s:e], val)
|
||||||
for i := s; i < e; i++ {
|
|
||||||
if n >= len(val) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
switch {
|
|
||||||
case b[i] >= 'A' && b[i] <= 'Z' && ('a'+(b[i]-'A')) != val[n]:
|
|
||||||
return false
|
|
||||||
case b[i] != val[n]:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(b []byte, s Pos, e Pos, val []byte) bool {
|
func contains(b []byte, s Pos, e Pos, chars string) bool {
|
||||||
for i := s; i < e; i++ {
|
return bytes.ContainsAny(b[s:e], chars)
|
||||||
for n := 0; n < len(val); n++ {
|
|
||||||
if b[i] == val[n] {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lowercase(b []byte, s Pos, e Pos) {
|
func lowercase(b []byte, s Pos, e Pos) {
|
||||||
|
|
|
@ -3,10 +3,9 @@ package qcode
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/maphash"
|
||||||
"sync"
|
"sync"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/dosco/super-graph/core/internal/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -35,8 +34,7 @@ const (
|
||||||
NodeVar
|
NodeVar
|
||||||
)
|
)
|
||||||
|
|
||||||
type Operation struct {
|
type SelectionSet struct {
|
||||||
Type parserType
|
|
||||||
Name string
|
Name string
|
||||||
Args []Arg
|
Args []Arg
|
||||||
argsA [10]Arg
|
argsA [10]Arg
|
||||||
|
@ -44,12 +42,29 @@ type Operation struct {
|
||||||
fieldsA [10]Field
|
fieldsA [10]Field
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Operation struct {
|
||||||
|
Type parserType
|
||||||
|
SelectionSet
|
||||||
|
}
|
||||||
|
|
||||||
var zeroOperation = Operation{}
|
var zeroOperation = Operation{}
|
||||||
|
|
||||||
func (o *Operation) Reset() {
|
func (o *Operation) Reset() {
|
||||||
*o = zeroOperation
|
*o = zeroOperation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Fragment struct {
|
||||||
|
Name string
|
||||||
|
On string
|
||||||
|
SelectionSet
|
||||||
|
}
|
||||||
|
|
||||||
|
var zeroFragment = Fragment{}
|
||||||
|
|
||||||
|
func (f *Fragment) Reset() {
|
||||||
|
*f = zeroFragment
|
||||||
|
}
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
ID int32
|
ID int32
|
||||||
ParentID int32
|
ParentID int32
|
||||||
|
@ -82,6 +97,8 @@ func (n *Node) Reset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
|
frags map[uint64]*Fragment
|
||||||
|
h maphash.Hash
|
||||||
input []byte // the string being scanned
|
input []byte // the string being scanned
|
||||||
pos int
|
pos int
|
||||||
items []item
|
items []item
|
||||||
|
@ -96,12 +113,194 @@ var opPool = sync.Pool{
|
||||||
New: func() interface{} { return new(Operation) },
|
New: func() interface{} { return new(Operation) },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fragPool = sync.Pool{
|
||||||
|
New: func() interface{} { return new(Fragment) },
|
||||||
|
}
|
||||||
|
|
||||||
var lexPool = sync.Pool{
|
var lexPool = sync.Pool{
|
||||||
New: func() interface{} { return new(lexer) },
|
New: func() interface{} { return new(lexer) },
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(gql []byte) (*Operation, error) {
|
func Parse(gql []byte) (*Operation, error) {
|
||||||
return parseSelectionSet(gql)
|
var err error
|
||||||
|
|
||||||
|
if len(gql) == 0 {
|
||||||
|
return nil, errors.New("blank query")
|
||||||
|
}
|
||||||
|
|
||||||
|
l := lexPool.Get().(*lexer)
|
||||||
|
l.Reset()
|
||||||
|
defer lexPool.Put(l)
|
||||||
|
|
||||||
|
if err = lex(l, gql); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &Parser{
|
||||||
|
input: l.input,
|
||||||
|
pos: -1,
|
||||||
|
items: l.items,
|
||||||
|
}
|
||||||
|
|
||||||
|
op := opPool.Get().(*Operation)
|
||||||
|
op.Reset()
|
||||||
|
op.Fields = op.fieldsA[:0]
|
||||||
|
|
||||||
|
s := -1
|
||||||
|
qf := false
|
||||||
|
|
||||||
|
for {
|
||||||
|
if p.peek(itemEOF) {
|
||||||
|
p.ignore()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemFragment) {
|
||||||
|
p.ignore()
|
||||||
|
if err = p.parseFragment(op); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !qf && p.peek(itemQuery, itemMutation, itemSub, itemObjOpen) {
|
||||||
|
s = p.pos
|
||||||
|
qf = true
|
||||||
|
}
|
||||||
|
p.ignore()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.reset(s)
|
||||||
|
if err := p.parseOp(op); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return op, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseFragment(op *Operation) error {
|
||||||
|
frag := fragPool.Get().(*Fragment)
|
||||||
|
frag.Reset()
|
||||||
|
|
||||||
|
frag.Fields = frag.fieldsA[:0]
|
||||||
|
frag.Args = frag.argsA[:0]
|
||||||
|
|
||||||
|
if p.peek(itemName) {
|
||||||
|
frag.Name = p.val(p.next())
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemOn) {
|
||||||
|
p.ignore()
|
||||||
|
} else {
|
||||||
|
return errors.New("fragment: missing 'on' keyword")
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemName) {
|
||||||
|
frag.On = p.vall(p.next())
|
||||||
|
} else {
|
||||||
|
return errors.New("fragment: missing table name after 'on' keyword")
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemObjOpen) {
|
||||||
|
p.ignore()
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("fragment: expecting a '{', got: %s", p.next())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := p.parseSelectionSet(&frag.SelectionSet); err != nil {
|
||||||
|
return fmt.Errorf("fragment: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.frags == nil {
|
||||||
|
p.frags = make(map[uint64]*Fragment)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = p.h.WriteString(frag.Name)
|
||||||
|
k := p.h.Sum64()
|
||||||
|
p.h.Reset()
|
||||||
|
|
||||||
|
p.frags[k] = frag
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseOp(op *Operation) error {
|
||||||
|
var err error
|
||||||
|
var typeSet bool
|
||||||
|
|
||||||
|
if p.peek(itemQuery, itemMutation, itemSub) {
|
||||||
|
err = p.parseOpTypeAndArgs(op)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%s: %v", op.Type, err)
|
||||||
|
}
|
||||||
|
typeSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemObjOpen) {
|
||||||
|
p.ignore()
|
||||||
|
if !typeSet {
|
||||||
|
op.Type = opQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if p.peek(itemEOF, itemFragment) {
|
||||||
|
p.ignore()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.parseSelectionSet(&op.SelectionSet)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%s: %v", op.Type, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("expecting a query, mutation or subscription, got: %s", p.next())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseOpTypeAndArgs(op *Operation) error {
|
||||||
|
item := p.next()
|
||||||
|
|
||||||
|
switch item._type {
|
||||||
|
case itemQuery:
|
||||||
|
op.Type = opQuery
|
||||||
|
case itemMutation:
|
||||||
|
op.Type = opMutate
|
||||||
|
case itemSub:
|
||||||
|
op.Type = opSub
|
||||||
|
}
|
||||||
|
|
||||||
|
op.Args = op.argsA[:0]
|
||||||
|
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if p.peek(itemName) {
|
||||||
|
op.Name = p.val(p.next())
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.peek(itemArgsOpen) {
|
||||||
|
p.ignore()
|
||||||
|
|
||||||
|
op.Args, err = p.parseOpParams(op.Args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseSelectionSet(selset *SelectionSet) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
selset.Fields, err = p.parseFields(selset.Fields)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseArgValue(argVal string) (*Node, error) {
|
func ParseArgValue(argVal string) (*Node, error) {
|
||||||
|
@ -123,216 +322,107 @@ func ParseArgValue(argVal string) (*Node, error) {
|
||||||
return op, err
|
return op, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSelectionSet(gql []byte) (*Operation, error) {
|
func (p *Parser) parseFields(fields []Field) ([]Field, error) {
|
||||||
var err error
|
st := NewStack()
|
||||||
|
|
||||||
if len(gql) == 0 {
|
if !p.peek(itemName, itemSpread) {
|
||||||
return nil, errors.New("blank query")
|
return nil, fmt.Errorf("unexpected token: %s", p.peekNext())
|
||||||
}
|
}
|
||||||
|
|
||||||
l := lexPool.Get().(*lexer)
|
for {
|
||||||
l.Reset()
|
if p.peek(itemEOF) {
|
||||||
|
|
||||||
if err = lex(l, gql); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &Parser{
|
|
||||||
input: l.input,
|
|
||||||
pos: -1,
|
|
||||||
items: l.items,
|
|
||||||
}
|
|
||||||
|
|
||||||
var op *Operation
|
|
||||||
|
|
||||||
if p.peek(itemObjOpen) {
|
|
||||||
p.ignore()
|
p.ignore()
|
||||||
op, err = p.parseQueryOp()
|
return nil, errors.New("invalid query")
|
||||||
} else {
|
|
||||||
op, err = p.parseOp()
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.peek(itemObjClose) {
|
if p.peek(itemObjClose) {
|
||||||
p.ignore()
|
p.ignore()
|
||||||
|
|
||||||
|
if st.Len() != 0 {
|
||||||
|
st.Pop()
|
||||||
|
continue
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("operation missing closing '}'")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !p.peek(itemEOF) {
|
|
||||||
p.ignore()
|
|
||||||
return nil, fmt.Errorf("invalid '%s' found after closing '}'", p.current())
|
|
||||||
}
|
|
||||||
|
|
||||||
lexPool.Put(l)
|
|
||||||
|
|
||||||
return op, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) next() item {
|
|
||||||
n := p.pos + 1
|
|
||||||
if n >= len(p.items) {
|
|
||||||
p.err = errEOT
|
|
||||||
return item{_type: itemEOF}
|
|
||||||
}
|
|
||||||
p.pos = n
|
|
||||||
return p.items[p.pos]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) ignore() {
|
|
||||||
n := p.pos + 1
|
|
||||||
if n >= len(p.items) {
|
|
||||||
p.err = errEOT
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.pos = n
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) current() string {
|
|
||||||
item := p.items[p.pos]
|
|
||||||
return b2s(p.input[item.pos:item.end])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) peek(types ...itemType) bool {
|
|
||||||
n := p.pos + 1
|
|
||||||
// if p.items[n]._type == itemEOF {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
|
|
||||||
if n >= len(p.items) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for i := 0; i < len(types); i++ {
|
|
||||||
if p.items[n]._type == types[i] {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) parseOp() (*Operation, error) {
|
|
||||||
if !p.peek(itemQuery, itemMutation, itemSub) {
|
|
||||||
err := errors.New("expecting a query, mutation or subscription")
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
item := p.next()
|
|
||||||
|
|
||||||
op := opPool.Get().(*Operation)
|
|
||||||
op.Reset()
|
|
||||||
|
|
||||||
switch item._type {
|
|
||||||
case itemQuery:
|
|
||||||
op.Type = opQuery
|
|
||||||
case itemMutation:
|
|
||||||
op.Type = opMutate
|
|
||||||
case itemSub:
|
|
||||||
op.Type = opSub
|
|
||||||
}
|
|
||||||
|
|
||||||
op.Fields = op.fieldsA[:0]
|
|
||||||
op.Args = op.argsA[:0]
|
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if p.peek(itemName) {
|
|
||||||
op.Name = p.val(p.next())
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.peek(itemArgsOpen) {
|
|
||||||
p.ignore()
|
|
||||||
|
|
||||||
op.Args, err = p.parseOpParams(op.Args)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if p.peek(itemObjOpen) {
|
|
||||||
p.ignore()
|
|
||||||
|
|
||||||
for n := 0; n < 10; n++ {
|
|
||||||
if !p.peek(itemName) {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
op.Fields, err = p.parseFields(op.Fields)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return op, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) parseQueryOp() (*Operation, error) {
|
|
||||||
op := opPool.Get().(*Operation)
|
|
||||||
op.Reset()
|
|
||||||
|
|
||||||
op.Type = opQuery
|
|
||||||
op.Fields = op.fieldsA[:0]
|
|
||||||
op.Args = op.argsA[:0]
|
|
||||||
|
|
||||||
var err error
|
|
||||||
|
|
||||||
for n := 0; n < 10; n++ {
|
|
||||||
if !p.peek(itemName) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
op.Fields, err = p.parseFields(op.Fields)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return op, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) parseFields(fields []Field) ([]Field, error) {
|
|
||||||
st := util.NewStack()
|
|
||||||
|
|
||||||
for {
|
|
||||||
if len(fields) >= maxFields {
|
if len(fields) >= maxFields {
|
||||||
return nil, fmt.Errorf("too many fields (max %d)", maxFields)
|
return nil, fmt.Errorf("too many fields (max %d)", maxFields)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.peek(itemEOF, itemObjClose) {
|
isFrag := false
|
||||||
p.ignore()
|
|
||||||
st.Pop()
|
|
||||||
|
|
||||||
if st.Len() == 0 {
|
if p.peek(itemSpread) {
|
||||||
break
|
p.ignore()
|
||||||
} else {
|
isFrag = true
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !p.peek(itemName) {
|
if !p.peek(itemName) {
|
||||||
return nil, errors.New("expecting an alias or field name")
|
if isFrag {
|
||||||
|
return nil, fmt.Errorf("expecting a fragment name, got: %s", p.next())
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("expecting an alias or field name, got: %s", p.next())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var f *Field
|
||||||
|
|
||||||
|
if isFrag {
|
||||||
|
name := p.val(p.next())
|
||||||
|
p.h.WriteString(name)
|
||||||
|
k := p.h.Sum64()
|
||||||
|
p.h.Reset()
|
||||||
|
|
||||||
|
fr, ok := p.frags[k]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("no fragment named '%s' defined", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
n := int32(len(fields))
|
||||||
|
fields = append(fields, fr.Fields...)
|
||||||
|
|
||||||
|
for i := int(n); i < len(fields); i++ {
|
||||||
|
f := &fields[i]
|
||||||
|
f.ID = int32(i)
|
||||||
|
|
||||||
|
// If this is the top-level point the parent to the parent of the
|
||||||
|
// previous field.
|
||||||
|
if f.ParentID == -1 {
|
||||||
|
pid := st.Peek()
|
||||||
|
f.ParentID = pid
|
||||||
|
if f.ParentID != -1 {
|
||||||
|
fields[pid].Children = append(fields[f.ParentID].Children, f.ID)
|
||||||
|
}
|
||||||
|
// Update all the other parents id's by our new place in this new array
|
||||||
|
} else {
|
||||||
|
f.ParentID += n
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update all the children which is needed.
|
||||||
|
for j := range f.Children {
|
||||||
|
f.Children[j] += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
fields = append(fields, Field{ID: int32(len(fields))})
|
fields = append(fields, Field{ID: int32(len(fields))})
|
||||||
|
|
||||||
f := &fields[(len(fields) - 1)]
|
f = &fields[(len(fields) - 1)]
|
||||||
f.Args = f.argsA[:0]
|
f.Args = f.argsA[:0]
|
||||||
f.Children = f.childrenA[:0]
|
f.Children = f.childrenA[:0]
|
||||||
|
|
||||||
// Parse the inside of the the fields () parentheses
|
// Parse the field
|
||||||
// in short parse the args like id, where, etc
|
|
||||||
if err := p.parseField(f); err != nil {
|
if err := p.parseField(f); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
intf := st.Peek()
|
if st.Len() == 0 {
|
||||||
if pid, ok := intf.(int32); ok {
|
f.ParentID = -1
|
||||||
|
} else {
|
||||||
|
pid := st.Peek()
|
||||||
f.ParentID = pid
|
f.ParentID = pid
|
||||||
fields[pid].Children = append(fields[pid].Children, f.ID)
|
fields[pid].Children = append(fields[pid].Children, f.ID)
|
||||||
} else {
|
}
|
||||||
f.ParentID = -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The first opening curley brackets after this
|
// The first opening curley brackets after this
|
||||||
|
@ -340,13 +430,6 @@ func (p *Parser) parseFields(fields []Field) ([]Field, error) {
|
||||||
if p.peek(itemObjOpen) {
|
if p.peek(itemObjOpen) {
|
||||||
p.ignore()
|
p.ignore()
|
||||||
st.Push(f.ID)
|
st.Push(f.ID)
|
||||||
|
|
||||||
} else if p.peek(itemObjClose) {
|
|
||||||
if st.Len() == 0 {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,6 +629,62 @@ func (p *Parser) vall(v item) string {
|
||||||
return b2s(p.input[v.pos:v.end])
|
return b2s(p.input[v.pos:v.end])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) peek(types ...itemType) bool {
|
||||||
|
n := p.pos + 1
|
||||||
|
l := len(types)
|
||||||
|
// if p.items[n]._type == itemEOF {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
if n >= len(p.items) {
|
||||||
|
return types[0] == itemEOF
|
||||||
|
}
|
||||||
|
|
||||||
|
if l == 1 {
|
||||||
|
return p.items[n]._type == types[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
if p.items[n]._type == types[i] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) next() item {
|
||||||
|
n := p.pos + 1
|
||||||
|
if n >= len(p.items) {
|
||||||
|
p.err = errEOT
|
||||||
|
return item{_type: itemEOF}
|
||||||
|
}
|
||||||
|
p.pos = n
|
||||||
|
return p.items[p.pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) ignore() {
|
||||||
|
n := p.pos + 1
|
||||||
|
if n >= len(p.items) {
|
||||||
|
p.err = errEOT
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p.pos = n
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) peekCurrent() string {
|
||||||
|
item := p.items[p.pos]
|
||||||
|
return b2s(p.input[item.pos:item.end])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) peekNext() string {
|
||||||
|
item := p.items[p.pos+1]
|
||||||
|
return b2s(p.input[item.pos:item.end])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) reset(to int) {
|
||||||
|
p.pos = to
|
||||||
|
}
|
||||||
|
|
||||||
func b2s(b []byte) string {
|
func b2s(b []byte) string {
|
||||||
return *(*string)(unsafe.Pointer(&b))
|
return *(*string)(unsafe.Pointer(&b))
|
||||||
}
|
}
|
||||||
|
@ -579,7 +718,7 @@ func (t parserType) String() string {
|
||||||
case NodeList:
|
case NodeList:
|
||||||
v = "node-list"
|
v = "node-list"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("<%s>", v)
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
// type Frees struct {
|
// type Frees struct {
|
||||||
|
|
|
@ -121,7 +121,7 @@ updateThread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`
|
}}`
|
||||||
qcompile, _ := NewCompiler(Config{})
|
qcompile, _ := NewCompiler(Config{})
|
||||||
_, err := qcompile.Compile([]byte(gql), "anon")
|
_, err := qcompile.Compile([]byte(gql), "anon")
|
||||||
|
|
||||||
|
@ -131,19 +131,90 @@ updateThread {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFragmentsCompile(t *testing.T) {
|
func TestFragmentsCompile1(t *testing.T) {
|
||||||
gql := `
|
gql := `
|
||||||
fragment userFields on user {
|
fragment userFields1 on user {
|
||||||
name
|
id
|
||||||
email
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
|
created_at
|
||||||
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}
|
||||||
|
`
|
||||||
|
qcompile, _ := NewCompiler(Config{})
|
||||||
|
_, err := qcompile.Compile([]byte(gql), "user")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query { users { ...userFields } }`
|
func TestFragmentsCompile2(t *testing.T) {
|
||||||
qcompile, _ := NewCompiler(Config{})
|
gql := `
|
||||||
_, err := qcompile.Compile([]byte(gql), "anon")
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
if err == nil {
|
created_at
|
||||||
t.Fatal(errors.New("expecting an error"))
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields1 on user {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}`
|
||||||
|
qcompile, _ := NewCompiler(Config{})
|
||||||
|
_, err := qcompile.Compile([]byte(gql), "user")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFragmentsCompile3(t *testing.T) {
|
||||||
|
gql := `
|
||||||
|
fragment userFields1 on user {
|
||||||
|
id
|
||||||
|
email
|
||||||
|
}
|
||||||
|
|
||||||
|
fragment userFields2 on user {
|
||||||
|
first_name
|
||||||
|
last_name
|
||||||
|
}
|
||||||
|
|
||||||
|
query {
|
||||||
|
users {
|
||||||
|
...userFields2
|
||||||
|
|
||||||
|
created_at
|
||||||
|
...userFields1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
`
|
||||||
|
qcompile, _ := NewCompiler(Config{})
|
||||||
|
_, err := qcompile.Compile([]byte(gql), "user")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,7 +272,6 @@ func BenchmarkQCompileP(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkParse(b *testing.B) {
|
func BenchmarkParse(b *testing.B) {
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
|
|
Loading…
Reference in New Issue