Skip to content

Commit 0bd4ee4

Browse files
committed
--wip-- [skip ci]
1 parent 1aa30e6 commit 0bd4ee4

File tree

13 files changed

+205
-37
lines changed

13 files changed

+205
-37
lines changed

pkg/engine/TEMPLATE.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine
2+
3+
var T = &engine{
4+
Name: "",
5+
Imprints: []imprint{
6+
{
7+
Query: "",
8+
Matcher: inResponseText([]string{""}),
9+
},
10+
},
11+
}

pkg/engine/_agoo.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine
2+
3+
var Agoo = &engine{
4+
Name: "agoo",
5+
Imprints: []imprint{
6+
{
7+
Query: "query { zzz }",
8+
Matcher: inSection("code", []string{"eval error"}),
9+
},
10+
},
11+
}

pkg/engine/adriane.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package engine
22

33
var Adriane = &engine{
4-
Name: "adriane",
4+
Name: "Adriane",
55
Imprints: []imprint{
66
{
77
Query: "",

pkg/engine/apollo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package engine
22

33
var Apollo = &engine{
4-
Name: "apollo",
4+
Name: "Apollo",
55
Imprints: []imprint{
66
{
77
Query: "query @deprecated { __typename }",

pkg/engine/aws_app_sync.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine
2+
3+
var AWSAppSync = &engine{
4+
Name: "AWSAppSync",
5+
Imprints: []imprint{
6+
{
7+
Query: "query @skip { __typename }",
8+
Matcher: inResponseText([]string{"MisplacedDirective"}),
9+
},
10+
},
11+
}

pkg/engine/engine.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package engine
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
)
7+
8+
type engine struct {
9+
Name string
10+
Imprints []imprint
11+
}
12+
13+
type imprint struct {
14+
Query string
15+
Matcher matcher
16+
}
17+
18+
// A responseMatcher is a function that takes a response body and returns true if the response matches the engine.
19+
type matcher func(responseBody *[]byte) bool
20+
21+
// inResponseText returns a responseMatcher that checks if the response body contains any of the given strings.
22+
func inResponseText(matches []string) matcher {
23+
return func(responseBody *[]byte) bool {
24+
for _, match := range matches {
25+
if bytes.Contains(*responseBody, []byte(match)) {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
}
32+
33+
// inSection returns a responseMatcher that checks if the response body contains any of the given strings in the given section.
34+
func inSection(section string, matches []string) matcher {
35+
return func(responseBody *[]byte) bool {
36+
var reponseBody map[string]interface{}
37+
json.Unmarshal(*responseBody, &reponseBody)
38+
content, err := json.Marshal(reponseBody[section])
39+
if err != nil {
40+
return false
41+
}
42+
for _, match := range matches {
43+
if bytes.Contains(content, []byte(match)) {
44+
return true
45+
}
46+
}
47+
return false
48+
}
49+
}
50+
51+
// hasJsonKey returns a responseMatcher that checks if the response body contains the given key.
52+
func hasJsonKey(key string) matcher {
53+
return func(responseBody *[]byte) bool {
54+
var reponseBody map[string]interface{}
55+
json.Unmarshal(*responseBody, &reponseBody)
56+
_, ok := reponseBody[key]
57+
return ok
58+
}
59+
}
60+
61+
// Order is important here, as the first match will be returned.
62+
// The order has been determined by the usage statistics of the engines. (The higher the usage, the higher the priority.)
63+
var Engines = []*engine{
64+
Apollo,
65+
AWSAppSync,
66+
GraphQLGo,
67+
Ruby,
68+
GraphQLPHP,
69+
Graphene,
70+
Adriane,
71+
GraphQLGopherGo,
72+
}

pkg/engine/engines.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

pkg/engine/graphene.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine
2+
3+
var Graphene = &engine{
4+
Name: "Graphene",
5+
Imprints: []imprint{
6+
{
7+
Query: "query { aaa }",
8+
Matcher: inResponseText([]string{"Syntax Error GraphQL (1:1)"}),
9+
},
10+
},
11+
}

pkg/engine/graphql_go.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package engine
2+
3+
var GraphQLGo = &engine{
4+
Name: "GraphQLGo",
5+
Imprints: []imprint{
6+
{
7+
Query: "",
8+
Matcher: inResponseText([]string{"Must provide an operation."}),
9+
},
10+
{
11+
Query: "query { __typename {}",
12+
Matcher: inResponseText([]string{"Unexpected empty IN"}),
13+
},
14+
{
15+
Query: "query { __typename }",
16+
Matcher: inResponseText([]string{"RootQuery"}),
17+
},
18+
},
19+
}

pkg/engine/graphql_gopher_go.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package engine
2+
3+
var GraphQLGopherGo = &engine{
4+
Name: "",
5+
Imprints: []imprint{
6+
{
7+
Query: "query {}",
8+
Matcher: hasJsonKey("data"),
9+
},
10+
},
11+
}

0 commit comments

Comments
 (0)