Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/openapi/fixtures/v3.0.0/EmployeesApi.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ type Query {
Get an employee
Uses the getEmployee Db2 z/OS asset
"""
employeesIdGet(id: String): EmployeeBody
employeesIdGet(id: String!): EmployeeBody
}

type Mutation {
"""
Delete an employee
Uses the deleteEmployee Db2 z/OS asset
"""
employeesIdDelete(id: String): EmployeeNumber
employeesIdDelete(id: String!): EmployeeNumber
"""
Update an employee
Uses the updateEmployee Db2 z/OS asset
"""
employeesIdPut(employeeBodyInput: EmployeeBodyInput): EmployeeBody
employeesIdPut(employeeBodyInput: EmployeeBodyInput, id: String!): EmployeeBody
"""
Add an employee
Uses the addEmployee Db2 z/OS asset
Expand Down
2 changes: 1 addition & 1 deletion pkg/openapi/fixtures/v3.0.0/EmployeesApiBasic.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Query {

type Mutation {
"Uses the updateEmployee Db2 z/OS asset"
putEmployeesId(employeeBodyInput: EmployeeBodyInput): EmployeeBody
putEmployeesId(employeeBodyInput: EmployeeBodyInput, id: String!): EmployeeBody
}

type EmployeeBody {
Expand Down
6 changes: 3 additions & 3 deletions pkg/openapi/fixtures/v3.0.0/example_oas3.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ schema {

type Query {
"Return an author."
author(authorId: String): Author
author(authorId: String!): Author
"Return a book."
book(bookId: String): Book
book(bookId: String!): Book
"Return the author's next work."
nextWork(authorId: String): NextWork
nextWork(authorId: String!): NextWork
}

"An author"
Expand Down
14 changes: 7 additions & 7 deletions pkg/openapi/fixtures/v3.0.0/example_oas7.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema {

type Query {
"Find a device by name."
findDeviceByName(deviceName: String): Device
findDeviceByName(deviceName: String!): Device
"Return a device collection."
findDevices: [Device]
"Return a user."
Expand All @@ -14,23 +14,23 @@ type Query {

type Mutation {
"Create and return a device."
createDevice(deviceInput: DeviceInput): Device
createDevice(deviceInput: DeviceInput!): Device
"Replace a device by name."
replaceDeviceByName(deviceInput: DeviceInput): Device
replaceDeviceByName(deviceInput: DeviceInput!, deviceName: String!): Device
}

type Device {
"The device name in the network"
name: String
name: String!
status: Boolean
"The device owner Name"
userName: String
userName: String!
}

input DeviceInput {
name: String
name: String!
status: Boolean
userName: String
userName: String!
}

"A user represents a natural person"
Expand Down
12 changes: 6 additions & 6 deletions pkg/openapi/fixtures/v3.0.0/petstore-expanded.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ schema {

type Query {
"Returns a user based on a single ID, if the user does not have access to the pet"
findPetById(id: Int): Pet
findPetById(id: Int!): Pet
"""
Returns all pets from the system that the user has access to
Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.
Expand All @@ -17,18 +17,18 @@ type Query {

type Mutation {
"Creates a new pet in the store. Duplicates are allowed"
addPet(newPetInput: NewPetInput): Pet
addPet(newPetInput: NewPetInput!): Pet
"deletes a single pet based on the ID supplied"
deletePet(id: Int): String
deletePet(id: Int!): String
}

input NewPetInput {
name: String
name: String!
tag: String
}

type Pet {
id: Int
name: String
id: Int!
name: String!
tag: String
}
6 changes: 3 additions & 3 deletions pkg/openapi/fixtures/v3.0.0/petstore.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Query {
"List all pets"
listPets(limit: Int): [Pet]
"Info for a specific pet"
showPetById(petId: String): Pet
showPetById(petId: String!): Pet
}

type Mutation {
Expand All @@ -16,7 +16,7 @@ type Mutation {
}

type Pet {
id: Int
name: String
id: Int!
name: String!
tag: String
}
70 changes: 50 additions & 20 deletions pkg/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ func getTypeRef(kind string) (introspection.TypeRef, error) {
return introspection.TypeRef{}, fmt.Errorf("unknown type: %s", kind)
}

func isNonNullable(name string, required []string) bool {
for _, item := range required {
if item == name {
return true
}
}
return false
}

func convertToNonNull(ofType *introspection.TypeRef) introspection.TypeRef {
copiedOfType := *ofType
nonNullType := introspection.TypeRef{Kind: 2}
nonNullType.OfType = &copiedOfType
nonNullType.Name = copiedOfType.Name
return nonNullType
}

func getParamTypeRef(kind string) (introspection.TypeRef, error) {
// See introspection_enum.go
switch kind {
Expand Down Expand Up @@ -124,8 +141,8 @@ func extractFullTypeNameFromRef(ref string) string {
return strcase.ToCamel(parsed[len(parsed)-1])
}

func (c *converter) processSchemaProperties(fullType *introspection.FullType, schemas openapi3.Schemas) error {
for name, schemaRef := range schemas {
func (c *converter) processSchemaProperties(fullType *introspection.FullType, schema openapi3.Schema) error {
for name, schemaRef := range schema.Properties {
gqlType, err := c.getGraphQLTypeName(schemaRef)
if err != nil {
return err
Expand All @@ -136,6 +153,10 @@ func (c *converter) processSchemaProperties(fullType *introspection.FullType, sc
return err
}
typeRef.Name = &gqlType
if isNonNullable(name, schema.Required) {
typeRef = convertToNonNull(&typeRef)
}

field := introspection.Field{
Name: name,
Type: typeRef,
Expand All @@ -160,8 +181,10 @@ func (c *converter) processInputFields(ft *introspection.FullType, schemaRef *op
if err != nil {
return err
}

typeRef.Name = &gqlType
if isNonNullable(propertyName, schemaRef.Value.Required) {
typeRef = convertToNonNull(&typeRef)
}
f := introspection.InputValue{
Name: propertyName,
Type: typeRef,
Expand All @@ -188,14 +211,14 @@ func (c *converter) processArray(schema *openapi3.SchemaRef) error {
}
typeOfElements := schema.Value.Items.Value.Type
if typeOfElements == "object" {
err := c.processSchemaProperties(&ft, schema.Value.Items.Value.Properties)
err := c.processSchemaProperties(&ft, *schema.Value.Items.Value)
if err != nil {
return err
}
} else {
for _, item := range schema.Value.Items.Value.AllOf {
if item.Value.Type == "object" {
err := c.processSchemaProperties(&ft, item.Value.Properties)
err := c.processSchemaProperties(&ft, *item.Value)
if err != nil {
return err
}
Expand All @@ -219,7 +242,7 @@ func (c *converter) processObject(schema *openapi3.SchemaRef) error {
Name: fullTypeName,
Description: schema.Value.Description,
}
err := c.processSchemaProperties(&ft, schema.Value.Properties)
err := c.processSchemaProperties(&ft, *schema.Value)
if err != nil {
return err
}
Expand Down Expand Up @@ -346,7 +369,7 @@ func getJSONSchemaFromRequestBody(operation *openapi3.Operation) *openapi3.Schem
return nil
}

func (c *converter) importQueryTypeFieldParameter(field *introspection.Field, name string, schema *openapi3.SchemaRef) error {
func (c *converter) importQueryTypeFieldParameter(field *introspection.Field, parameter *openapi3.Parameter, schema *openapi3.SchemaRef) error {
paramType := schema.Value.Type
if paramType == "array" {
paramType = schema.Value.Items.Value.Type
Expand All @@ -356,7 +379,6 @@ func (c *converter) importQueryTypeFieldParameter(field *introspection.Field, na
if err != nil {
return err
}

gqlType, err := getPrimitiveGraphQLTypeName(paramType)
if err != nil {
return err
Expand All @@ -373,8 +395,11 @@ func (c *converter) importQueryTypeFieldParameter(field *introspection.Field, na
}

typeRef.Name = &gqlType
if parameter.Required {
typeRef = convertToNonNull(&typeRef)
}
iv := introspection.InputValue{
Name: name,
Name: parameter.Name,
Type: typeRef,
}

Expand Down Expand Up @@ -403,7 +428,7 @@ func (c *converter) importQueryTypeFields(typeRef *introspection.TypeRef, operat
if schema == nil {
continue
}
err := c.importQueryTypeFieldParameter(&f, parameter.Value.Name, schema)
err := c.importQueryTypeFieldParameter(&f, parameter.Value, schema)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -440,8 +465,7 @@ func (c *converter) importQueryType() (*introspection.FullType, error) {
if schema == nil {
continue
}
getJSONSchema(status, operation)
kind := getJSONSchema(status, operation).Value.Type
kind := schema.Value.Type
if kind == "" {
// We assume that it is an object type.
kind = "object"
Expand All @@ -456,7 +480,6 @@ func (c *converter) importQueryType() (*introspection.FullType, error) {
// Array of some type
typeRef.OfType = &introspection.TypeRef{Kind: 3, Name: &typeName}
}

typeRef.Name = &typeName
queryField, err := c.importQueryTypeFields(&typeRef, operation)
if err != nil {
Expand Down Expand Up @@ -571,16 +594,23 @@ func (c *converter) importMutationType() (*introspection.FullType, error) {
if err != nil {
return nil, err
}
if operation.RequestBody.Value.Required {
inputValue.Type = convertToNonNull(&inputValue.Type)
}
f.Args = append(f.Args, *inputValue)
} else {
for _, parameter := range operation.Parameters {
inputValue, err = c.addParameters(parameter.Value.Name, parameter.Value.Schema)
if err != nil {
return nil, err
}
f.Args = append(f.Args, *inputValue)
}

for _, parameter := range operation.Parameters {
inputValue, err = c.addParameters(parameter.Value.Name, parameter.Value.Schema)
if err != nil {
return nil, err
}
if parameter.Value.Required {
inputValue.Type = convertToNonNull(&inputValue.Type)
}
f.Args = append(f.Args, *inputValue)
}

sort.Slice(f.Args, func(i, j int) bool {
return f.Args[i].Name < f.Args[j].Name
})
Expand Down