Skip to content

Commit 647a796

Browse files
committed
Handle multi-files
1 parent 92b261d commit 647a796

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
"@netlify/build": "^26.3.14",
4343
"@netlify/config": "^17.0.18",
4444
"@netlify/framework-info": "^9.0.2",
45-
"netlify-onegraph-internal": "0.1.18"
45+
"netlify-onegraph-internal": "0.2.0"
4646
}
4747
}

src/index.js

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
} from 'netlify-onegraph-internal';
1111
import path from 'path';
1212

13+
const { Kind, parse, print } = GraphQL
14+
1315
/**
1416
* Remove any relative path components from the given path
1517
* @param {string[]} items Filesystem path items to filter
@@ -43,6 +45,10 @@ const makeDefaultNetlifyGraphConfig = ({
4345
...netlifyGraphPath,
4446
NetlifyGraph.defaultSourceOperationsFilename,
4547
];
48+
const graphQLOperationsSourceDirectory = [
49+
...netlifyGraphPath,
50+
NetlifyGraph.defaultSourceOperationsDirectoryName,
51+
];
4652
const graphQLSchemaFilename = [
4753
...netlifyGraphPath,
4854
NetlifyGraph.defaultGraphQLSchemaFilename,
@@ -56,6 +62,7 @@ const makeDefaultNetlifyGraphConfig = ({
5662
netlifyGraphPath,
5763
netlifyGraphImplementationFilename,
5864
netlifyGraphTypeDefinitionsFilename,
65+
graphQLOperationsSourceDirectory,
5966
graphQLOperationsSourceFilename,
6067
graphQLSchemaFilename,
6168
netlifyGraphRequirePath,
@@ -94,6 +101,7 @@ const makeDefaultNextJsNetlifyGraphConfig = ({ baseConfig, siteRoot }) => {
94101
...netlifyGraphPath,
95102
NetlifyGraph.defaultGraphQLSchemaFilename,
96103
];
104+
const graphQLOperationsSourceDirectory = [...netlifyGraphPath, ...NetlifyGraph.defaultSourceOperationsDirectoryName]
97105
const netlifyGraphRequirePath = ['..', '..', 'lib', 'netlifyGraph'];
98106
const moduleType = baseConfig.moduleType || 'esm';
99107

@@ -103,6 +111,7 @@ const makeDefaultNextJsNetlifyGraphConfig = ({ baseConfig, siteRoot }) => {
103111
netlifyGraphPath,
104112
netlifyGraphImplementationFilename,
105113
netlifyGraphTypeDefinitionsFilename,
114+
graphQLOperationsSourceDirectory,
106115
graphQLOperationsSourceFilename,
107116
graphQLSchemaFilename,
108117
netlifyGraphRequirePath,
@@ -144,6 +153,8 @@ const makeDefaultRemixNetlifyGraphConfig = ({
144153
...netlifyGraphPath,
145154
NetlifyGraph.defaultGraphQLSchemaFilename,
146155
];
156+
const graphQLOperationsSourceDirectory = [...netlifyGraphPath, ...NetlifyGraph.defaultSourceOperationsDirectoryName]
157+
147158
const netlifyGraphRequirePath = [`../../netlify/functions/netlifyGraph`];
148159
const moduleType = 'esm';
149160

@@ -153,6 +164,7 @@ const makeDefaultRemixNetlifyGraphConfig = ({
153164
netlifyGraphPath,
154165
netlifyGraphImplementationFilename,
155166
netlifyGraphTypeDefinitionsFilename,
167+
graphQLOperationsSourceDirectory,
156168
graphQLOperationsSourceFilename,
157169
graphQLSchemaFilename,
158170
netlifyGraphRequirePath,
@@ -319,23 +331,66 @@ const readGraphQLSchemaFile = (netlifyGraphConfig) => {
319331
/**
320332
* Using the given NetlifyGraphConfig, read the GraphQL operations file and return the _unparsed_ GraphQL operations doc
321333
* @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
322-
* @returns {string} GraphQL operations doc
334+
* @returns {string | null} GraphQL operations doc
323335
*/
324-
const readGraphQLOperationsSourceFile = (netlifyGraphConfig) => {
325-
ensureNetlifyGraphPath(netlifyGraphConfig);
336+
const readLegacyGraphQLOperationsSourceFile = (netlifyGraphConfig) => {
337+
ensureNetlifyGraphPath(netlifyGraphConfig)
326338

327-
const fullFilename = path.resolve(
328-
...netlifyGraphConfig.graphQLOperationsSourceFilename
329-
);
339+
const fullFilename = path.resolve(...netlifyGraphConfig.graphQLOperationsSourceFilename)
330340
if (!fs.existsSync(fullFilename)) {
331-
fs.writeFileSync(fullFilename, '');
332-
fs.closeSync(fs.openSync(fullFilename, 'w'));
341+
return null
333342
}
334343

335-
const source = fs.readFileSync(fullFilename, 'utf8');
344+
const source = fs.readFileSync(fullFilename, 'utf8')
336345

337-
return source;
338-
};
346+
return source
347+
}
348+
349+
/**
350+
* Using the given NetlifyGraphConfig, read all of the GraphQL operation files and return the _unparsed_ GraphQL operations doc
351+
* @param {NetlifyGraph.NetlifyGraphConfig} netlifyGraphConfig
352+
* @returns {string | null} GraphQL operations doc
353+
*/
354+
const readGraphQLOperationsSourceFiles = (netlifyGraphConfig) => {
355+
ensureNetlifyGraphPath(netlifyGraphConfig)
356+
357+
const operationsPath = path.resolve(...netlifyGraphConfig.graphQLOperationsSourceDirectory)
358+
359+
const operationFiles = []
360+
361+
const filenames = fs.readdirSync(operationsPath)
362+
filenames.forEach((filename) => {
363+
if (/.*\.(graphql?)/gi.test(filename)) {
364+
const content = fs.readFileSync(path.resolve(operationsPath, filename), 'utf8')
365+
const file = {
366+
name: filename,
367+
path: path.resolve(operationsPath, filename),
368+
content,
369+
parsedOperation: parse(content),
370+
}
371+
372+
operationFiles.push(file)
373+
}
374+
})
375+
376+
const emptyDocDefinitionNode = {
377+
kind: Kind.DOCUMENT,
378+
definitions: [],
379+
}
380+
381+
const parsedDoc = operationFiles.reduce((acc, file) => {
382+
const { parsedOperation } = file
383+
const { definitions } = parsedOperation
384+
return {
385+
kind: Kind.DOCUMENT,
386+
definitions: [...acc.definitions, ...definitions],
387+
}
388+
}, emptyDocDefinitionNode)
389+
390+
const source = print(parsedDoc)
391+
392+
return source
393+
}
339394

340395
const generatePersistedFunctionsFile = async ({
341396
fragments,
@@ -466,7 +521,6 @@ Run \`netlify graph:init\` to generate a new token.`
466521
}
467522
);
468523
}
469-
const schemaString = readGraphQLSchemaFile(netlifyGraphConfig);
470524

471525
console.log('Creating a new Netlify Graph schema');
472526

@@ -499,6 +553,8 @@ Run \`netlify graph:init\` to generate a new token.`
499553

500554
let schema;
501555

556+
const schemaString = readGraphQLSchemaFile(netlifyGraphConfig);
557+
502558
try {
503559
schema = GraphQL.buildSchema(schemaString);
504560
} catch (buildSchemaError) {
@@ -510,8 +566,14 @@ Run \`netlify graph:init\` to generate a new token.`
510566
return;
511567
}
512568

569+
const legacySourceGraphQLFile = readLegacyGraphQLOperationsSourceFile(netlifyGraphConfig)
570+
571+
if (legacySourceGraphQLFile) {
572+
build.failBuild('Found legacy single-file operations library. Run `netlify graph:library` to migrate');
573+
}
574+
513575
let currentOperationsDoc =
514-
readGraphQLOperationsSourceFile(netlifyGraphConfig);
576+
readGraphQLOperationsSourceFiles(netlifyGraphConfig);
515577
if (currentOperationsDoc.trim().length === 0) {
516578
console.warn(
517579
'No Graph operations library found, skipping production client generation.'

0 commit comments

Comments
 (0)