Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): Remove accidental capitalization of lowercase typenames. #12837

Merged
merged 3 commits into from
Mar 26, 2019
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
26 changes: 26 additions & 0 deletions packages/gatsby/src/schema/__tests__/build-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,32 @@ describe(`Build schema`, () => {
})
)
})

it(`allows modifying nested types`, async () => {
createTypes(`
type PostFrontmatter {
published: Boolean!
newField: String
}

type Post implements Node {
frontmatter: PostFrontmatter
}
`)

const schema = await buildSchema()

const type = schema.getType(`Post`)
const fields = type.getFields()
expect(fields[`frontmatter`].type.toString()).toEqual(`PostFrontmatter`)
const nestedType = schema.getType(`PostFrontmatter`)
const nestedFields = nestedType.getFields()
expect(nestedFields[`authors`].type.toString()).toEqual(`[String]`)
expect(nestedFields[`reviewers`].type.toString()).toEqual(`[String]`)
expect(nestedFields[`published`].type.toString()).toEqual(`Boolean!`)
expect(nestedFields[`date`].type.toString()).toEqual(`Date`)
expect(nestedFields[`newField`].type.toString()).toEqual(`String`)
})
})

describe(`createResolvers`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,27 @@ Object {
}
`;

exports[`GraphQL type inference handles lowercase type names 1`] = `
Object {
"data": Object {
"allWordpressPage": Object {
"edges": Array [
Object {
"node": Object {
"__typename": "wordpress__PAGE",
"acfFields": Object {
"__typename": "wordpress__PAGEAcfFields",
"fooz": "bar",
},
"id": "1",
},
},
],
},
},
}
`;

exports[`GraphQL type inference type conflicts catches conflicts and removes field 1`] = `
Array [
TypeConflictEntry {
Expand Down
44 changes: 44 additions & 0 deletions packages/gatsby/src/schema/infer/__tests__/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,50 @@ describe(`GraphQL type inference`, () => {
expect(result).toMatchSnapshot()
})

it(`handles lowercase type names`, async () => {
const nodes = [
{
id: `1`,
internal: { type: `wordpress__PAGE` },
acfFields: {
fooz: `bar`,
},
},
]
const schema = await buildTestSchema(nodes)
store.dispatch({ type: `SET_SCHEMA`, payload: schema })
const result = await graphql(
schema,
`
query {
allWordpressPage {
edges {
node {
__typename
id
acfFields {
fooz
__typename
}
}
}
}
}
`,
undefined,
{
path: `/`,
nodeModel: new LocalNodeModel({
schema,
nodeStore,
createPageDependency,
}),
}
)

expect(result).toMatchSnapshot()
})

describe(`Handles dates`, () => {
it(`Handles integer with valid date format`, async () => {
const nodes = [
Expand Down
10 changes: 5 additions & 5 deletions packages/gatsby/src/schema/infer/add-inferred-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const addInferredFieldsImpl = ({
.map(field => `\`${field.unsanitizedKey}\``)
.join(`, `)
report.warn(
`Multiple node fields resolve to the same GraphQL field \`${
`Multiple node fields resolve to the same GraphQL field \`${prefix}.${
field.key
}\` - [${possibleFieldsNames}]. Gatsby will use \`${
field.unsanitizedKey
Expand Down Expand Up @@ -385,12 +385,12 @@ const getSimpleFieldConfig = ({
}

const createTypeName = selector => {
const key = selector
.split(`.`)
const keys = selector.split(`.`)
const suffix = keys
.slice(1)
.map(_.upperFirst)
.join(``)

return key
return `${keys[0]}${suffix}`
}

const NON_ALPHA_NUMERIC_EXPR = new RegExp(`[^a-zA-Z0-9_]`, `g`)
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/infer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const addInferredTypes = ({
})

// XXX(freiksenet): We iterate twice to pre-create all types
const typeComposers = typeNames.map(typeName => {
const typeComposers = typeNames.map(typeName =>
addInferredType({
schemaComposer,
nodeStore,
Expand All @@ -77,7 +77,7 @@ const addInferredTypes = ({
typeMapping,
parentSpan,
})
})
)

if (noNodeInterfaceTypes.length > 0) {
noNodeInterfaceTypes.forEach(type => {
Expand Down