Skip to content

Commit

Permalink
Merge pull request #67 from vektah/raw-schema-string
Browse files Browse the repository at this point in the history
Use a raw string for schema
  • Loading branch information
vektah authored Mar 28, 2018
2 parents c0753be + af6178a commit 893ead1
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 22 deletions.
2 changes: 1 addition & 1 deletion codegen/templates/data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ type executionContext struct {
{{ template "input.gotpl" $input }}
{{- end }}

var parsedSchema = schema.MustParse({{.SchemaRaw|quote}})

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand All @@ -127,3 +125,5 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse({{.SchemaRaw|rawQuote}})
15 changes: 10 additions & 5 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

func Run(name string, tpldata interface{}) (*bytes.Buffer, error) {
t := template.New("").Funcs(template.FuncMap{
"ucFirst": ucFirst,
"lcFirst": lcFirst,
"quote": strconv.Quote,
"toCamel": toCamel,
"dump": dump,
"ucFirst": ucFirst,
"lcFirst": lcFirst,
"quote": strconv.Quote,
"rawQuote": rawQuote,
"toCamel": toCamel,
"dump": dump,
})

for filename, data := range data {
Expand Down Expand Up @@ -80,6 +81,10 @@ func toCamel(s string) string {
return string(buffer)
}

func rawQuote(s string) string {
return "`" + strings.Replace(s, "'", "`+\"`\"+`", -1) + "`"
}

func dump(val interface{}) string {
switch val := val.(type) {
case int:
Expand Down
29 changes: 27 additions & 2 deletions example/chat/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,6 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co
return ec.___Type(ctx, field.Selections, res)
}

var parsedSchema = schema.MustParse("type Chatroom {\n name: String!\n messages: [Message!]!\n}\n\ntype Message {\n id: ID!\n text: String!\n createdBy: String!\n createdAt: Time!\n}\n\ntype Query {\n room(name:String!): Chatroom\n}\n\ntype Mutation {\n post(text: String!, username: String!, roomName: String!): Message!\n}\n\ntype Subscription {\n messageAdded(roomName: String!): Message!\n}\n\nscalar Time\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand All @@ -868,3 +866,30 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse(`type Chatroom {
name: String!
messages: [Message!]!
}
type Message {
id: ID!
text: String!
createdBy: String!
createdAt: Time!
}
type Query {
room(name:String!): Chatroom
}
type Mutation {
post(text: String!, username: String!, roomName: String!): Message!
}
type Subscription {
messageAdded(roomName: String!): Message!
}
scalar Time
`)
35 changes: 33 additions & 2 deletions example/dataloader/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,6 @@ func (ec *executionContext) ___Type_ofType(ctx context.Context, field graphql.Co
return ec.___Type(ctx, field.Selections, res)
}

var parsedSchema = schema.MustParse("type Query {\n customers: [Customer!]\n\n # this method is here to test code generation of nested arrays\n torture(customerIds: [[Int]]): [[Customer!]]\n}\n\ntype Customer {\n id: Int!\n name: String!\n address: Address\n orders: [Order!]\n}\n\ntype Address {\n id: Int!\n street: String!\n country: String!\n}\n\ntype Order {\n id: Int!\n date: Time!\n amount: Float!\n items: [Item!]\n}\n\ntype Item {\n name: String!\n}\nscalar Time\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand All @@ -906,3 +904,36 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse(`type Query {
customers: [Customer!]
# this method is here to test code generation of nested arrays
torture(customerIds: [[Int]]): [[Customer!]]
}
type Customer {
id: Int!
name: String!
address: Address
orders: [Order!]
}
type Address {
id: Int!
street: String!
country: String!
}
type Order {
id: Int!
date: Time!
amount: Float!
items: [Item!]
}
type Item {
name: String!
}
scalar Time
`)
32 changes: 30 additions & 2 deletions example/scalars/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,6 @@ func UnmarshalSearchArgs(v interface{}) (SearchArgs, error) {
return it, nil
}

var parsedSchema = schema.MustParse("type Query {\n user(id: ID!): User\n search(input: SearchArgs = {location: \"37,144\"}): [User!]!\n}\n\ntype User {\n id: ID!\n name: String!\n created: Timestamp\n isBanned: Boolean!\n primitiveResolver: String!\n customResolver: Point!\n address: Address\n}\n\ntype Address {\n id: ID!\n location: Point\n}\n\ninput SearchArgs {\n location: Point\n createdAfter: Timestamp\n isBanned: Boolean\n}\n\nscalar Timestamp\nscalar Point\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand All @@ -869,3 +867,33 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse(`type Query {
user(id: ID!): User
search(input: SearchArgs = {location: "37,144"}): [User!]!
}
type User {
id: ID!
name: String!
created: Timestamp
isBanned: Boolean!
primitiveResolver: String!
customResolver: Point!
address: Address
}
type Address {
id: ID!
location: Point
}
input SearchArgs {
location: Point
createdAfter: Timestamp
isBanned: Boolean
}
scalar Timestamp
scalar Point
`)
28 changes: 26 additions & 2 deletions example/selection/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,6 @@ func (ec *executionContext) _Event(ctx context.Context, sel []query.Selection, o
}
}

var parsedSchema = schema.MustParse("interface Event {\n selection: [String!]\n collected: [String!]\n}\n\ntype Post implements Event {\n message: String!\n sent: Time!\n selection: [String!]\n collected: [String!]\n}\n\ntype Like implements Event {\n reaction: String!\n sent: Time!\n selection: [String!]\n collected: [String!]\n}\n\ntype Query {\n events: [Event]\n}\n\nscalar Time\n")

func (ec *executionContext) introspectSchema() *introspection.Schema {
return introspection.WrapSchema(parsedSchema)
}
Expand All @@ -766,3 +764,29 @@ func (ec *executionContext) introspectType(name string) *introspection.Type {
}
return introspection.WrapType(t)
}

var parsedSchema = schema.MustParse(`interface Event {
selection: [String!]
collected: [String!]
}
type Post implements Event {
message: String!
sent: Time!
selection: [String!]
collected: [String!]
}
type Like implements Event {
reaction: String!
sent: Time!
selection: [String!]
collected: [String!]
}
type Query {
events: [Event]
}
scalar Time
`)
Loading

0 comments on commit 893ead1

Please sign in to comment.