Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@
"$ref": "#/$defs/RemotePersistConfig"
},
{
"description": "This variant represents a local persistence configuration, where GraphQL queries are persisted to a local JSON file.\n\nWhen this variant is used, the compiler will attempt to read the local file as a hash map,\nadd new queries to the map, and then serialize and write the resulting map to the configured path.",
"description": "This variant represents a local persistence configuration, where GraphQL queries are persisted to a local JSON file.\n\nWhen this variant is used, the compiler will attempt to read the local file as a hash map,\nadd new queries to the map, and then serialize and write the resulting map to the configured path.\nThe file (and any missing parent directories) is created if it does not already exist.",
"$ref": "#/$defs/LocalPersistConfig"
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::collections::BTreeMap;
use std::fs::File;
use std::io::BufWriter;
use std::io::ErrorKind;
use std::io::Write;

use async_trait::async_trait;
Expand Down Expand Up @@ -37,10 +38,16 @@ impl LocalPersister {
pub fn new(config: LocalPersistConfig) -> Self {
let query_map: DashMap<String, String> = match std::fs::read_to_string(&config.file) {
Ok(content) => serde_json::from_str(&content).unwrap_or_default(),
Err(_e) => {
Err(e) if e.kind() == ErrorKind::NotFound => {
// First run: the file doesn't exist yet. Start with an empty
// map; `finalize` creates the file.
Default::default()
}
Err(e) => {
panic!(
"LocalPersister: Expected the {} file to exist.",
"LocalPersister: Unable to read the {} file: {}",
&config.file.display(),
e,
)
}
};
Expand Down Expand Up @@ -91,6 +98,13 @@ impl OperationPersister for LocalPersister {
.map(|x| (x.key().clone(), x.value().clone()))
.collect();

// `Path::parent` returns `Some("")` for a bare relative file name like
// `persisted_queries.json`, and `create_dir_all("")` fails.
if let Some(parent) = self.config.file.parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
let mut writer = BufWriter::new(File::create(&self.config.file)?);
serde_json::to_writer_pretty(&mut writer, &ordered)?;
writer.write_all(b"\n")?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
==================================== INPUT ====================================
//- src/component.js

graphql`query componentQuery {
me {
name
}
}`

//- relay.config.json
{
"sources": {
"src": "test_project"
},
"projects": {
"test_project": {
"schema": "schema.graphql",
"language": "javascript",
"persist": {
"file": "./persisted_queries.json",
"algorithm": "MD5"
}
}
}
}

//- schema.graphql
type Query {
me: User
}

type User {
name: String
}
==================================== OUTPUT ===================================
//-++ persisted_queries.json
{
"a70bad875245c0b80e06c2084c9fbff5": "query componentQuery {\n me {\n name\n }\n}\n"
}

//-++ src/__generated__/componentQuery.graphql.js
/**
* <auto-generated> SignedSource<<5e6e5f0ef8b7ed3b7ff8eabb168ed5a0>>
* @relayHash a70bad875245c0b80e06c2084c9fbff5
* @lightSyntaxTransform
*/

/* eslint-disable */

'use strict';

// @relayRequestID a70bad875245c0b80e06c2084c9fbff5

var node = (function(){
var v0 = [
{
"alias": null,
"args": null,
"concreteType": "User",
"kind": "LinkedField",
"name": "me",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
}
],
"storageKey": null
}
];
return {
"fragment": {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "componentQuery",
"selections": (v0/*:: as any*/),
"type": "Query",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": [],
"kind": "Operation",
"name": "componentQuery",
"selections": (v0/*:: as any*/)
},
"params": {
"id": "a70bad875245c0b80e06c2084c9fbff5",
"metadata": {},
"name": "componentQuery",
"operationKind": "query",
"text": null
}
};
})();

node.hash = "57e4a4c3f66e23aaa4883404c0a73a32";

export default node;



Artifact Map:
Project: test_project
Type: Mapping
- Source: ExecutableDefinition: componentQuery
Path: src/__generated__/componentQuery.graphql.js
Persisted ID: a70bad875245c0b80e06c2084c9fbff5
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//- src/component.js

graphql`query componentQuery {
me {
name
}
}`

//- relay.config.json
{
"sources": {
"src": "test_project"
},
"projects": {
"test_project": {
"schema": "schema.graphql",
"language": "javascript",
"persist": {
"file": "./persisted_queries.json",
"algorithm": "MD5"
}
}
}
}

//- schema.graphql
type Query {
me: User
}

type User {
name: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
==================================== INPUT ====================================
//- src/component.js

graphql`query componentQuery {
me {
name
}
}`

//- relay.config.json
{
"sources": {
"src": "test_project"
},
"projects": {
"test_project": {
"schema": "schema.graphql",
"language": "javascript",
"persist": {
"file": "./persisted_queries.json",
"algorithm": "MD5"
}
}
}
}

//- persisted_queries.json
{
"00000000000000000000000000000000": "query previouslyPersistedQuery {\n me {\n name\n }\n}\n"
}

//- schema.graphql
type Query {
me: User
}

type User {
name: String
}
==================================== OUTPUT ===================================
//-++ persisted_queries.json
{
"00000000000000000000000000000000": "query previouslyPersistedQuery {\n me {\n name\n }\n}\n",
"a70bad875245c0b80e06c2084c9fbff5": "query componentQuery {\n me {\n name\n }\n}\n"
}

//-++ src/__generated__/componentQuery.graphql.js
/**
* <auto-generated> SignedSource<<5e6e5f0ef8b7ed3b7ff8eabb168ed5a0>>
* @relayHash a70bad875245c0b80e06c2084c9fbff5
* @lightSyntaxTransform
*/

/* eslint-disable */

'use strict';

// @relayRequestID a70bad875245c0b80e06c2084c9fbff5

var node = (function(){
var v0 = [
{
"alias": null,
"args": null,
"concreteType": "User",
"kind": "LinkedField",
"name": "me",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "name",
"storageKey": null
}
],
"storageKey": null
}
];
return {
"fragment": {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "componentQuery",
"selections": (v0/*:: as any*/),
"type": "Query",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": [],
"kind": "Operation",
"name": "componentQuery",
"selections": (v0/*:: as any*/)
},
"params": {
"id": "a70bad875245c0b80e06c2084c9fbff5",
"metadata": {},
"name": "componentQuery",
"operationKind": "query",
"text": null
}
};
})();

node.hash = "57e4a4c3f66e23aaa4883404c0a73a32";

export default node;



Artifact Map:
Project: test_project
Type: Mapping
- Source: ExecutableDefinition: componentQuery
Path: src/__generated__/componentQuery.graphql.js
Persisted ID: a70bad875245c0b80e06c2084c9fbff5
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//- src/component.js

graphql`query componentQuery {
me {
name
}
}`

//- relay.config.json
{
"sources": {
"src": "test_project"
},
"projects": {
"test_project": {
"schema": "schema.graphql",
"language": "javascript",
"persist": {
"file": "./persisted_queries.json",
"algorithm": "MD5"
}
}
}
}

//- persisted_queries.json
{
"00000000000000000000000000000000": "query previouslyPersistedQuery {\n me {\n name\n }\n}\n"
}

//- schema.graphql
type Query {
me: User
}

type User {
name: String
}
Loading