Jump to content
dadish

GraphQL for ProcessWire

Recommended Posts

14 hours ago, blynx said:

The confusion might be about this:

Normally in processwire templates you have to "make the fields public" by manually echoing data in a template (echo $page->title) - so actually for a guest user everything is hidden by default - though by permission actually authorized.

With this module - everything gets "unveiled" (to use another term here) automatically. This is what I meant by "public" and "private".

... am I right?

Ooh, that's right. Now I get what you mean. Thank you for clarifying that for me.

That's true, with this module it gets available to the public without echoing it explicitly. So you will have to setup extra permissions to make it closed to the guest user. This was the initial intention of this module really. The goal is to build a tool that will allow you to quickly bootstrap an AJAX api of your ProcessWire content to build SPA out of it. For cases like you guys describe, this module might have some drawbacks. But you could always cook your own GraphQL api and make it behave however you want. It's fairly easy after you learn a bit about it. Here is the library I use for this module.

  • Like 5

Share this post


Link to post
Share on other sites

in regard to the public / private part, what @blynx mentioned, especially the by PW default "private fields" simply needs to be covered via accessrights on fieldlevel, set by the dev who wants to use this module. There is no need for public/private endpoints in the module. Or I'm wrong?

If you build a site without that module, you are done by simply use display logic in your templatefiles to control output of fields. If you want to use the module, you additionally have to mimik the accessrights via PW fields access settings. Thats how I understand it by just reading this really valuable thread. :)

@Nurguly Ashyrov ^-^

  • Like 4

Share this post


Link to post
Share on other sites
5 minutes ago, horst said:

in regard to the public / private part, what @blynx mentioned, especially the by PW default "private fields" simply needs to be covered via accessrights on fieldlevel, set by the dev who wants to use this module. There is no need for public/private endpoints in the mosule. Or I'm wrong?

If you build a site without that module, you are done by simply use display logic in your templatefiles to control output of fields. If you want to use the module, you additionally have to mimik the accessrights via PW fields access settings. Thats how I understand it by just reading this really valuable thread. :)

That's right. That's the way it will work when module is done. Currently there is a support for template permissions. Support for Field permissions are on the way, I am working on it but it will take some time. For now you can limit the fields via module settings, I pushed support for legal fields two hours ago.

12 minutes ago, horst said:

Thumbs up from @horst ! Yeeeey :) I am a big fan of your modules!

  • Like 7

Share this post


Link to post
Share on other sites
2 hours ago, bernhard said:

hi @Nurguly Ashyrov 

this looks very interesting!

could you please provide a simple example how one would use graphql and/or your module with simple ajax requests? i guess this could be great to return data for https://datatables.net/ ?

Hey there @bernhard! I am happy people like it.

To make an AJAX request to your GraphQL api, you need to send a query variable that describes what you want. You should've seen how the query looks like in the first post of this thread. To send the query you can use any of your favorite AJAX libraries. Here is how it looks like in jQuery.

$.post(
  '/graphql/', // this is the url where your GraphQL api is exposed.
  {
    query: "{ skyscrapers { list { title } } }", // this is your query, this one requests the skyscrapers and title field for each of them.
  },
  function (res) { console.log(res); } // here you do whatever you want with the returned data
);

Or if you prefer to communicate with your api in application/json, you can do that too. Here is how it looks like in my favorite AJAX library - superagent.

superagent
  .post('/graphql/')
  .set('Content-Type', 'application/json')
  .send({
    query: "{ city { list { title } } }"
  })
  .then(function (res) {
	console.log(res.body);
  });

PLEASE NOTE:  When making requests to your api make sure the url is pointing where it is supposed to. The request will not work if you omit the trailing slash. With default settings the request to /graphql won't be processed by ProcessWire but instead redirected to /graphql/ and the ProcessGraphQL module will not receive the query parameter you sent. Same might happen with leading www. prefix. The request to http://example.com will be redirected to http://www.example.com which will also loose the query parameter in the middle.

  • Like 8

Share this post


Link to post
Share on other sites

thank you for the quick response! i got that now :)

hmmm... so you define your query on the client side? how can you make sure that people do not modify your queries in a way that you do not want? i read of your server-side restrictions regarding templates, but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, bernhard said:

but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

For some websites this might even be the goal. But I can understand your reasoning. It might be interesting to be able to use the module to create endpoints with a somewhat fixed queries. Once the experimental phase is over and it goes towards production queries could be moved from the frontend to the backend.

 

  • Like 5

Share this post


Link to post
Share on other sites
12 hours ago, bernhard said:

hmmm... so you define your query on the client side? how can you make sure that people do not modify your queries in a way that you do not want? i read of your server-side restrictions regarding templates, but wouldn't it still be possible to modify the queries to some extend (like changing number of records to show, fields and so on). i'm thinking of someone maybe stealing content or creating his own json exports of my site's content...

That will all depend on how you configure your api. If you wish this module can expose all pages in your website, including the system ones. Or you can restrict to some very limited data. There are more functionality to come,

but at this stage the module supports:

  • Template restrictions. You can choose which templates are enabled, but in addition it will conform to ProcessWire permissions. So you could limit access to certain templates only to certain user roles. You can enable access to only logged in users for example.
  • Field restrictions. Also supports ProcessWire permissions. Including template context permissions. Meaning, you can allow title to be viewable for one template and restrict for another.
  • Max limit support. Like the one you use in selectors. So users won't be able to get list of data for more than say 50 pages at a time.

and also many other security options are on the roadmap: 

  • Built in field restrictions. At it's current stage api gives access to page fields like children, parents, createdUser..., or there are path, size, basename for files and images fields. Those will be disabled and available as extras.
  • Query complexity limit. Currently you can build queries as deep as you want, to request ridiculous amounts of data. This will also be limited for only couple levels of complexity and you will be able to increase or decrease it.
  • Like 10

Share this post


Link to post
Share on other sites

@Nurguly Ashyrov

Hello Nurguly,

I was following this thread from day one and I am deeply impressed by the passion you are putting into your 'baby'. Especially your in-depth explanations helped me understand your concept and getting first results on my own. Very often talented programmers do not have the same skill in teaching others . But you definitely have.

Right now I am experimenting with your module to figure out if this could be a  method to be used in a PWA (progressive web application). Using the GraphQL channel I would reload just my 'new' data via the service worker . Therefore it wouldn't matter that this data is exposed to the public - it should go there anyhow.

 

  • Like 8

Share this post


Link to post
Share on other sites
On 2/5/2017 at 11:44 PM, Werner Pilnei said:

Hello Nurguly,

I was following this thread from day one and I am deeply impressed by the passion you are putting into your 'baby'. Especially your in-depth explanations helped me understand your concept and getting first results on my own. Very often talented programmers do not have the same skill in teaching others . But you definitely have.

Thank you @Werner Pilnei. I am excited because I like using ProcessWire :). I try to do my best in introducing this module to the community. GraphQL is very young standard and is not mainstream yet. I intentionally started this thread in the Pub section, to make sure this is not a module support page but more a discussion on GraphQL (as this new api standard by facebook) and ProcessWire. To talk about how they could fit with each other, what ways we could use it, the new ways to use ProcessWire and so on.

I personally never think about ProcessWire as a CMS. Though it is in fact a true CMS in its literal meaning, it is best at managing your content. But when people are introduced to ProcessWire it is presented as CMS and since the web is cursed by WordPress, people start using ProcessWire with wrong assumptions in their minds which result in negative impressions. I am generalizing here but when an average web developer hears CMS, she thinks it is a ready website with bunch of functionality baked in like tags, searching, blogging, commenting and so on.  Those functionalities become the evaluation criteria and when they see that there is no tags in ProcessWire they count that as one of the things ProcessWire is missing. They don't understand that tags are something ProcessWire shouldn't have, because they are used to see tags in a CMS.

I don't think that I am telling something new here. The community is well aware of this problem and the release of new site profile states that these problems are being addressed. But it doesn't have to be the only way. The modular architecture of ProcessWire allows us to extend it anyway we want, and this module is one of those attempts in presenting ProcessWire in different perspective. Even if it won't make much difference, I think we should keep trying and experimenting. Who knows what could come up along the way. I was only thinking about SPAs when creating this module. Never thought of PWA and usage with service workers like you approached it. Which is, by the way is great to hear. I hope there will be bunch of other ways people use it.

  • Like 19

Share this post


Link to post
Share on other sites

Install seems fine, both in 1st and 2sd attempts, no error msgs, but when Setup > graphQL this shows up:

 

Parse Error: syntax error, unexpected '^', expecting ']' (line 9 of /home/zota/www/pw-logica/site/modules/ProcessGraphQL/GraphiQL/build/static/js/main.059c7daf.js) 

Any clue?
thanks

  • Like 1

Share this post


Link to post
Share on other sites
6 hours ago, zota said:

Parse Error: syntax error, unexpected '^', expecting ']' (line 9 of /home/zota/www/pw-logica/site/modules/ProcessGraphQL/GraphiQL/build/static/js/main.059c7daf.js) 

Any clue?
thanks

Just to make sure. Is this a PHP error, or is it a JavaScript error in your browser console?

Could you also give some environment details. Which version of PHP, ProcessWire?

  • Like 1

Share this post


Link to post
Share on other sites

Hey @zota. I made some changes on how the GraphiQL assets are loaded in the admin interface. Before that it was a quick stupid hack, sorry. Now it is as it supposed to be loaded for Process modules. I don't know why I didn't do it from the start. Please remove the old version and install the latest from repository. The error you are getting will most likely go away.

  • Like 3

Share this post


Link to post
Share on other sites
2 hours ago, Nurguly Ashyrov said:

@zotaThe error you are getting will most likely go away.

You are right! Fine now! Thanks!

 

  • Like 2

Share this post


Link to post
Share on other sites

@Nurguly Ashyrov, this is really awesome. 

My front-end work is mostly JS/SPA work these days and I learnt to love the “headless”/“api first“ approach of commercial CMSes like Contentful. So I often thought about what a perfect and easy to manage back-end ProcessWire would be with a solid HTTP-based api built right in to allow uncoupling actual front-end/ui parts like admin and user interfaces (as actual self-contained apps) from the CMS itself.

A module like yours makes a huge leap in this direction and will make ProcessWire much more interesting for a lot of front-end devs and e.g. as a flexible and fast to set up back-end for app prototyping/mvp development. A pity this didn’t exist 9 month ago!

  • Like 11

Share this post


Link to post
Share on other sites

This is a fantastic module! Thank you Nurguly!

Does this handle Language fields yet? For example, I have translated titles, summaries for an "article"  template, and I've allowed the article template, along with the "title" and "summary" fields in the module settings, but the the translatable fields are still not available through GraphiQL, even though the other non-translatable fields are. 

 

  • Like 2

Share this post


Link to post
Share on other sites

@Oliver, @bcartier Thanks guys. Glad you like it.

3 hours ago, bcartier said:

Does this handle Language fields yet? For example, I have translated titles, summaries for an "article"  template, and I've allowed the article template, along with the "title" and "summary" fields in the module settings, but the the translatable fields are still not available through GraphiQL, even though the other non-translatable fields are. 

No, Language fields are not supported yet. There will be support for them too. Maybe I will implement them this weekend. In any case I will let you know when they are available.

For those who are following this module, I make releases almost everyday. Sometimes I introduce new bugs but patch them as soon as I find them. There are bunch of features added since the first introduction. You can follow up the changes/additions in the changelog. Lots of parts are not documented yet but I will provide full documentation and possibly introduction video targeted to ProcessWire users, on what the module is about, how you can use it, along with some todo app tutorial.

But for now, feel free to play with it and provide some feedback either here or in issue tracker. It is always easier to make changes in the beginning.

  • Like 9

Share this post


Link to post
Share on other sites

@Nurguly Ashyrov I really like what you do here! So much fun to see this. ^-^

Unfortunately I have no time ATM to start playing / using it. (buhuhh, - schnüff) :)

  • Like 4

Share this post


Link to post
Share on other sites
On 10/02/2017 at 7:49 PM, Oliver said:

@Nurguly Ashyrov

A module like yours makes a huge leap in this direction and will make ProcessWire much more interesting for a lot of front-end devs and e.g. as a flexible and fast to set up back-end for app prototyping/mvp development. A pity this didn’t exist 9 month ago!

Ditto this. I have been following this module development with huge interest. I too develop SPA with VueJS and this module will be very useful.

Hopefully not a stupid question, but is it possible, for example, to get cropped images? Or more of a general question, can you manipulate the data before you fetch it? Here is an example of the JSON I generate ATM:

{
	"banners":
	[
		{
			"origin": {
				"description": "",
				"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.jpg",
				"width": 2500,
				"height": 1666
			},
			"thumb": {
				"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.400x400.jpg",
				"width": 400,
				"height": 400,
				"orientation": ""
			},
			"bp": {
				"400": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.400x300.jpg"
				},
				"800": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.800x600.jpg"
				},
				"1200": {
					"httpUrl": "http://www.mysite.localdev/site/assets/files/1/img.1200x900.jpg"
				}
			},
			"srcset": "http://www.mysite.localdev/site/assets/files/1/img.400x300.jpg 400w,http://www.mysite.localdev/site/assets/files/1/img.800x600.jpg 800w,http://www.mysite.localdev/site/assets/files/1/img.1200x900.jpg 1200w"
		}
	]
}

 

  • Like 2

Share this post


Link to post
Share on other sites
14 minutes ago, microcipcip said:

is it possible, for example, to get cropped images? Or more of a general question, can you manipulate the data before you fetch it?

No there isn't at the moment. There will be support for it of course. The way it will work is if the user has a view access to the image field she will be able to fetch all the available image information, including the thumbnail variations. For user to be able to create thumbnails herself, she will need an edit access for that field.

  • Like 4

Share this post


Link to post
Share on other sites

Hey @bcartier and everyone who is following. I implemented a basic language support. Nothing really changed, except now with LanguageSupport enabled in your ProcessWire app, the GraphQL api will return the content in whatever language the user is assigned.

In addition, when Language Support module is activated, there is a language field in your GraphQL api. So you can request the exact language you want. It looks like this.

{
  language(name: "de")
  basic_page{
    list{
      title
      summary
    }
  }
}

You need to put the language field on the top. Well, not exactly on top but just before fields that return translatable content, like title, headline, body etc. It's because GraphQL processes requested fields from top to bottom and it will not know what language you want till it gets to language field. Did you also know that in GraphQL you can query same field multiple times with aliases? Here, take a look at this

{
  basic_page_default: basic_page{
    list{
      title
      summary
    }
  }
  language(name: "de")
  basic_page_de: basic_page{
    list{
      title
      summary
    }
  }
}

Curious what will be the response? Try this with site-languages  profile and find out.

  • Like 10

Share this post


Link to post
Share on other sites

Ah man, this is awesome. It'll make it a lot easier to use Processwire as the CMS for a front-end not built on Processwire. =D

  • Like 4

Share this post


Link to post
Share on other sites

Oh wow @Nurguly Ashyrov  this is an amazing effort. SO much potential usecases. Thanks for all your work.
Maybe and depending how you feel about it you could mention this modue in the modules section, I only accidently kind of stumbled upon it here in Offtopic > Pub. This deserves much wider recognition.

  • Like 1

Share this post


Link to post
Share on other sites

I am happy that you like it @Sebastian, thank you for your support. I started the thread here because I thought this would be more like a discussion on how GraphQL and ProcessWire could fit together and wanted to get some feedback first. But this thread quickly become this module's official place here in the ProcessWire forums. Also @teppo included the link to this thread as the "dedicated support forum thread" in the 143 issue of the weekly.pw (which I was flattered to see :)).

Now I don't really know how to go on with this thread. Should we abandon it and start new thread in the modules section? Or maybe this thread could be moved to modules section? What @moderators think of this?

Meanwhile, for those who are following this thread I wanted to mention that there are some additions in the dev branch, such as mutations that allows you to create/update pages and there is also support for FieldtypeMapMarker field. I stopped developing the module for some time because I thought that it needed a good testing before moving further with it and decided to built an SPA using this module, to see if there is something that need to be added or changed. But then I got carried away and started to make usage of third-party APIs such as Wikipedia and GoogleMaps. As a result the app does not make heavy usage of the ProcessGraphQL module, but it is still relevant to showcase the module's abilities. It is a US Skyscrapers app, duh... You can see it live here and the source code here (though I doubt that the code will interest you if you are not a React developer).

I was finished with this demo SPA just couple of days ago. Now I will be back to continue to work on this module again.

  • Like 11

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bernhard
      --- Please use RockFinder3 ---
    • By MoritzLost
      Cacheable Placeholders
      This module allows you to have pieces of dynamic content inside cached output. This aims to solve the common problem of having a mostly cacheable site, but with pieces of dynamic output here and there.  Consider this simple example, where you want to output a custom greeting to the current user:
      <h1>Good morning, <?= ucfirst($user->name) ?></h1> This snippet means you can't use the template cache (at least for logged-in users), because each user has a different name. Even if 99% of your output is static, you can only cache the pieces that you know won't include this personal greeting. A more common example would be CSRF tokens for HTML forms - those need to be unique by definition, so you can't cache the form wholesale.
      This module solves this problem by introducing cacheable placeholders - small placeholder tokens that get replaced during every request. The replacement is done inside a Page::render hook so it runs during every request, even if the response is served from the template cache. So you can use something like this:
      <h1>Good morning, {{{greeting}}}</h1> Replacement tokens are defined with a callback function that produces the appropriate output and added to the module through a simple hook:
      // site/ready.php wire()->addHookAfter('CachePlaceholders::getTokens', function (HookEvent $e) { $tokens = $e->return; $tokens['greeting'] = [ 'callback' => function (array $tokenData) { return ucfirst(wire('user')->name); } ]; $e->return = $tokens; }); Tokens can also include parameters that are parsed and passed to the callback function. There are more fully annotated examples and step-by-step instructions in the README on Github!
      Features
      A simple and fast token parser that calls the appropriate callback and runs automatically. Tokens may include multiple named or positional parameters, as well as multi-value parameters. A manual mode that allows you to replace tokens in custom pieces of cached content (useful if you're using the $cache API). Some built-in tokens for common use-cases: CSRF-Tokens, replacing values from superglobals and producing random hexadecimal strings. The token format is completely customizable, all delimiters can be changed to avoid collisions with existing tag parsers or template languages. Links
      Github Repository & documentation Module directory (pending approval) If you are interested in learning more, the README is very extensive, with more usage examples, code samples and usage instructions!
    • By Craig
      I've been using Fathom Analytics for a while now and on a growing number of sites, so thought it was about time there was a PW module for it.
      WayFathomAnalytics
      WayFathomAnalytics is a group of modules which will allow you to view your Fathom Analytics dashboard in the PW admin panel and (optionally) automatically add and configure the tracking code on front-end pages.
      Links
      GitHub Readme & documentation Download Zip Modules directory Module settings screenshot What is Fathom Analytics?
      Fathom Analytics is a simple, privacy-focused website analytics tool for bloggers and businesses.

      Stop scrolling through pages of reports and collecting gobs of personal data about your visitors, both of which you probably don't need. Fathom is a simple and private website analytics platform that lets you focus on what's important: your business.
      Privacy focused Fast-loading dashboards, all data is on a single screen Easy to get what you need, no training required Unlimited email reports Private or public dashboard sharing Cookie notices not required (it doesn't use cookies or collect personal data) Displays: top content, top referrers, top goals and more
    • By daniels
      This is a lightweight alternative to other newsletter & newsletter-subscription modules.
      You can find the Module in the Modules directory and on Github
      It can subscribe, update, unsubscribe & delete a user in a list in Mailchimp with MailChimp API 3.0. It does not provide any forms or validation, so you can feel free to use your own. To protect your users, it does not save any user data in logs or sends them to an admin.
      This module fits your needs if you...
      ...use Mailchimp as your newsletter / email-automation tool ...want to let users subscribe to your newsletter on your website ...want to use your own form, validation and messages (with or without the wire forms) ...don't want any personal user data saved in any way in your ProcessWire environment (cf. EU data regulation terms) ...like to subscribe, update, unsubscribe or delete users to/from different lists ...like the Mailchimp UI for creating / sending / reviewing email campaigns *I have only tested it with PHP 7.x so far, so use on owners risk
      EDIT:
      Since 0.0.4, instructions and changelog can be found in the README only. You can find it here  🙂
      If you have questions or like to contribute, just post a reply or create an issue or pr on github, thanks!
    • By MoritzLost
      Sorry for the convoluted title. I have a problem with Process modules that define a custom page using the page key through getModuleInfo (as demonstrated in this excellent tutorial by @bernhard). Those pages are created automatically when the module is installed. The problem is that the title of the page only gets set in the current language. That's not a problem if the current language (language of the superuser who is installing the module) is the default language; if it isn't, the Process page is missing a title in the default language. This has the very awkward effect that a user using the backend in the default language (or any other language) will see an empty entry in the setup menu:

      This screenshot comes from my Cache Control module which includes a Process page. Now I realize the description sounds obscure, but for us it's a common setup: We a multiple bilingual sites where the default language is German and the second language is English. While the clients use the CMS in German, as a developer I prefer the English interface, so whenever I install a Process module I get this problem.
      As a module author, is there a way to handle this situation? I guess it would be possible to use post-installation hooks or create the pages manually, but I very much prefer the declarative approach. The page title is already translatable (through the __ function), but of course at the time of installation there is no translation, and as far as I'm aware it's not possible to ship translations with a module so they are used automatically. Could this situation be handled better in the core? I would prefer if the module installation process would always set the title of the Process page in the default language, instead of the language of the current user.
×
×
  • Create New...